Auto login after registration (fake login)

SiteController::actionRegister();




	public function actionRegister()

	{

		if(!Yii::app()->getUser()->getIsGuest())

		{

			$this->redirect(array('/some url here'));

		}else{

			$registrationForm = new RegistrationForm;

			

			if(($post = Yii::app()->getRequest()->getPost('RegistrationForm')) !== null)

			{

				$registrationForm->attributes = $post;

				

				if($registrationForm->process())

				{

					$loginForm = new LoginForm;

					

					$loginForm->username = $registrationForm->username;

					$loginForm->password = $registrationForm->password;

					

					if($loginForm->process())

					{

						$this->redirect(Yii::app()->homeUrl);

					}else{

						$this->redirect(Yii::app()->getUser()->loginUrl);

					}

				}

			}

			

			$this->render('register', array(

				'form'=>$registrationForm,

			));

		}

	}



LoginForm




<?php


class LoginForm extends CFormModel

{

	public $username;

	public $password; 

	public $autologin;


	....

	

	public function process()

	{

		$this->validate();

		

		if(!$this->hasErrors())

		{

			$userIdentity = Yii::app()->user->getIdentityInstance($this->username, $this->password);

			

			if(!$userIdentity->authenticate())

			{

				if($userIdentity->errorCode === $userIdentity::ERROR_USERNAME_INVALID)

				{

					$this->addError('username', Yii::t('application', 'Username is invalid.'));

				}else if($userIdentity->errorCode === $userIdentity::ERROR_PASSWORD_INVALID)

				{

					$this->addError('password', Yii::t('application', 'Password is invalid.'));

				}else{

					$this->addError('username', Yii::t('application', 'We are unable to access your account at this time.'));

				}

				return false;

			}

			

			$period = 3600*24*7; // 7 days

			$duration = $this->autologin ? $period : 0;

			

			Yii::app()->getUser()->login($userIdentity, $duration);

			return true;

		}

		return false;

	}

	

	....

}



WebUser




<?php


class WebUser extends CWebUser

{

	public $allowAutoLogin = true;

	

	....

        public $identityClass = 'UserIdentity';

        ....

	

	public function getIdentityInstance($username=null, $password=null)

	{

		if(empty($this->identityClass))

		{

			throw new CException(Yii::t('application', 'Property Webuser.identityClass must be specified.'));

		}

	

		$className = Yii::import($this->identityClass);

		

		return new $className($username, $password);

	}

        ...........

}