Auto-Login User After Registration


I’m having trouble implementing a auto-login feature after user registration. I want to implement a feature that after registration, the user already has access to the application without having to fill out the login form.

On the User model, I added the following code:

    public function afterSave()



    	$identity=new UserIdentity($this->username,$this->password);




		    echo $identity->errorMessage;	


The added code has not changed the behavior: After the user is registered, it remains unauthenticated (no errors). The user then needs to fill the login form to enter the application.

The added code is correct? The location where the code was added (User model) is correct or should it be implemented elsewhere?


It definitely doesn’t belong in the User model. It should probably be handled at the end of the registration action, once the registration details have been confirmed and the user record has been created.

I have seen something similar in an activate account by replying to an email thing. The upshot was to have a special action that would do the login without a password. I don’t remember where I saw it, but google ‘Yii registration’ of something like that.

EDIT: It’s on Chris Brickhouse’s site for Cook your own User Authentication in Yii . Part 4 at the bottom is an actionActivate() and a modification to the UserIdentity to createAuthenticatedIdentity()

Hey jkofsky!

Thank you very much!

The solution is add the createAuthenticatedIdentity function on UserIdentity and calling it on UserController: Yii::app()->user->login(UserIdentity::createAuthenticatedIdentity($model->username,$model->id));

Good to hear and thanks for the +1 B)