0

I am trying to create a simple login using Yii Here is my auth controller

class AuthController  extends Controller
{
    /**
    * Declare class-based actions.
    */
    public function actionLogin()
    {
        $model = new LoginForm;
        $post = Yii::app()->request->getPost('LoginForm');
        // If form is submitted
        if($post) {
            $identity = new UserIdentity($post['username'], $post['password']);
            echo $identity->testing();
            if($identity->authenticate()) {
                echo 'yes';
            } else {
                echo 'no';
            }
            exit;
        }
        $this->render('login', array('model' => $model));   
    }
}

And here is my UserIdentity

class UserIdentity extends CUserIdentity
{


    private $_id;

    public function authenticate()
    {   echo 'testing';
        $user = LoginForm::model()->findByAttributes(array('username' => $this->username));
        if(is_null($user)) {
            %this->errorCode=self::ERROR_USERNAME_INVALID;
        } else if($user->password != $this->password) {
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        } else {
            $this->_id = $user->id;
            $this->errorCode=self::ERROR_NONE;
        }

        return !$this->errorCode;
    }

    function getId()
    {
        return $this->_id;
    }
}

I have mentioned echo 'yes' and echo 'no' but both are not displaying. How to correct it

Prabhakaran
  • 3,900
  • 15
  • 46
  • 113

1 Answers1

0

Well first of all, you won't even see those echo statements, the only things that are rendered visually for an end-user to see in Yii are the "views". For my login code, which is just a little bit different from yours, after confirming authentication, my app is redirected to home page. Your custom UserIdentity file looks fine, but again, that echo statement won't even be seen. This UserIdentity file is just for performing custom user authentication behind the scenes.

In my UserController (as opposed to your AuthController), my actionLogin is:

public function actionLogin()
{
    $model=new LoginForm;

    // if it is ajax validation request
    if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }

    // collect user input data
    if(isset($_POST['LoginForm']))
    {
        $model->attributes=$_POST['LoginForm'];
        // validate user input and redirect to the previous page if valid
        if($model->validate() && $model->login())               
        {
            $this->redirect(Yii::app()->user->returnUrl);
        }
    }
    $this->render('/user/login',array('model'=>$model));
}

From the above, for example, you could redirect to the previous page you were at or redirect to your main site view at "/site/index" and under that have some code that does some arbitrary functions or print out HTML depending on if you're logged in or not. An overly simple site view example:

<?php
/* @var $this SiteController */
if (Yii::app()->user->isGuest)
{
    // Do stuff here if user is guest.
    echo 'User is a guest.';
}
else
{
    // Do stuff here if user is authenticated.
    echo 'User is authenticated.';
}
?>
Andy Downs
  • 84
  • 4