David
David

Reputation: 4508

yii making user auto logged in on signup end (auto authentication after signup)

In yii there is Login action which takes

$model=new LoginForm; 

Where all login functions are stored. Like $model->login() which makes all authenticate procedures.

Now when you make signup you take different model

    $model=new User;

Because you must work with users database storing new users inside of it.

So how can I incorporate that LoginForm into $model during singup process to use it's functions ? Because I want the user who passes signup process became logged in automatically. and for that for example I need $model->login() code of which is stored inside LoginForm

UPDATE

Like I'vesaid in Login action there was a string if($model->validate() && $model->login())

So using link that provided Jonny I've updated a little my code from this

    if($model->validate())
    {
    if($model->save())

to look like this

if($model->validate())
            {
                $identity=new LoginForm($model->username,$model->password);
                $identity->login();
            if($model->save())

So now theoretically I'm making new object identity, passing username and password to it, and calling login function. Must be the same as in Login action. But this new born user is still not logging in.

here is Full Login action code

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);
        }
        // display the login form
        $this->render('login',array('model'=>$model));
    }

Also posting the code of Login function which is kept inside LoginForm

public function login()
    {
        if($this->_identity===null)
        {
            $this->_identity=new UserIdentity($this->username,$this->password);
            $this->_identity->authenticate();
        }
        if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
        {
            $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
            Yii::app()->user->login($this->_identity,$duration);
            return true;
        }
        else
            return false;
    } 

Upvotes: 1

Views: 549

Answers (1)

David
David

Reputation: 4508

Thanks for the Jonny, guys read the link he provided in comments, here is solution.

        $identity=new LoginForm();
        $identity->username=$model->username;
        $identity->password=$model->password;
        if($model->save()) {
            $identity->login();

Only send to identity username and model manually like me, not like in that link, and main thing register user in database before firing login function cause login function calls another guys to check your database for this new user.

Upvotes: 1

Related Questions