Yii Framework Forum: Auto login after registration (fake login) - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Auto login after registration (fake login) Rate Topic: ***** 1 Votes

#1 User is offline   Thomas Jensen 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 01-August 09

Posted 28 May 2010 - 05:06 AM

Hi there,

I just wondered which practice is the best for making an automatic login, after a user has registered himself.
Or if i, as admin, would like to login as another user, how do i then "fake" the login?

Thanks :)
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 28 May 2010 - 05:33 AM

I do it like this, after user has registered:


// $user == new created user record with Login and Password assigned from form
$identity=new UserIdentity($user->Login,$user->Password);
$identity->authenticate();
Yii::app()->user->login($identity,0);

1

#3 User is offline   Thomas Jensen 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 01-August 09

Posted 28 May 2010 - 06:14 AM

Okay, thanks, but you could imagine in some rare case, something extraordinary or if the password is encrypted, the authentication would fail.. I'm looking fore some 100% way of logging in a user.
0

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 28 May 2010 - 06:19 AM

Yeah, i thought about these issues, too.

About encryption: i usually add a public $Password to my user class and use CryptPassword as column name. CryptPassword is not visible in forms. I only overwrite it with encrypted password, if Password is set. So no problem with that.

Regarding these rare error condidtions: They should really only happen, if e.g. user record is updated in the same moment when login happens. To catch that you could add a line like:

if (Yii::app()->user->IsGuest)
  throw new CException('Something really dumb has happened: you could not get logged in after registration. Please use the login form to login.');

0

#5 User is offline   Thomas Jensen 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 01-August 09

Posted 28 May 2010 - 08:36 AM

I do something similar, but it's still not a perfect solution, and i can't use it to create an admin "simulate login" functionality.
0

#6 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 28 May 2010 - 08:44 AM

From checking the sources of CUserIdentity/CBaseUserIdentity, something like this should also work:

$identity=new UserIdentity($someUsername,'');
$identity->error=UserIdentiy::ERROR_NONE;

// to logout admin, if logged in:
if (!user()->isGuest)
  user()->logout();

user()->login($identity,0);

0

#7 User is offline   Thomas Jensen 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 01-August 09

Posted 28 May 2010 - 08:47 AM

Thats precisely what i do know, but again, it's not perfect, and could be broken by some later update.
0

#8 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 28 May 2010 - 08:52 AM

It should be update safe as it's pretty much the same what you do in authenticate() anyway. You could put that code into a forceLogin() method to your UserIdentity. So if things change one day, you only have to touch one class.
0

#9 User is offline   Spear 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 02-February 09

Posted 17 June 2010 - 08:29 AM

Mike is right. Thank you man for a good hint. Here is my working code:

$user=User::model()->findByPk($userID);
if($user===null) {
	throw new CHttpException(500, 'Bye');
} else {
	//...
	// Auto-login
	$identity=new UserIdentity($user->email,'');
	$identity->setID($user->id); /* had to add WebUser::setID() since WebUser::$_id is private */
	$identity->errorCode=UserIdentity::ERROR_NONE;
	Yii::app()->user->login($identity,0);
	//...
}

In my case auto-login failed right untill I added
$identity->setID($user->id);


Hope it'll help somebody. Love Yii!
1

#10 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 17 June 2010 - 08:49 AM

I've thought about a even cleaner solution. Actually i think it's the responsibility of UserIdentity to create a authenticated user. So a better implementation might be:

//UserIdentity:
public static function createAuthenticatedIdentity($id) {
    $identity=new self;
    $identity->setID($id);
    $identity->errorCode=self::ERROR_NONE;
    return $identity;
}

// Usage:
Yii::app()->user->login(UserIdentity::createAuthenticatedIdentity($userId),0);

2

#11 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 18 June 2010 - 07:32 AM

The solution of Mike is really good and clean.

Another option is to add a boolena parameter to authenticate checkPassword.

But maybe the method of Mike is more elegant.
0

#12 User is offline   suriyansuresh 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 23-July 10
  • Location:Coimbatore, India

Posted 25 October 2010 - 12:27 AM

it will produces an error

Missing argument 1 for CUserIdentity::__construct(), called in C:\wamp\www\tcc\protected\modules\customer\components\UserIdentity.php on line 48 and defined

View Postzaccaria, on 18 June 2010 - 07:32 AM, said:

The solution of Mike is really good and clean.

Another option is to add a boolena parameter to authenticate checkPassword.

But maybe the method of Mike is more elegant.

Suriyan

Yii application development | Web design
0

#13 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 25 October 2010 - 03:42 AM

Change the method to:



    public static function createAuthenticatedIdentity($username)
    {
        $identity=new self($username,'');
        $identity->errorCode=self::ERROR_NONE;
        return $identity;
    }


EDIT: Changed $id to $username. If you have more attributes in your UserIdentiy class that you want to set, you could add more arguments to this method and set them here, too.

This post has been edited by Mike: 01 December 2010 - 03:40 AM

3

#14 User is offline   nuxtech 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 23-October 10

Posted 30 November 2010 - 01:23 PM

View PostMike, on 25 October 2010 - 03:42 AM, said:

Change the method to:



    public static function createAuthenticatedIdentity($id)
    {
        $identity=new self($id,'');
        $identity->id=$id;
        $identity->errorCode=self::ERROR_NONE;
        return $identity;
    }



i try to implemented your solution mike.
but it gives me an error
Property "UserIdentity.id" is read only

0

#15 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 01 December 2010 - 03:40 AM

Ok, tried to make the example above more generic. Can you try again?
0

#16 User is offline   nuxtech 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 23-October 10

Posted 02 December 2010 - 11:51 AM

View PostMike, on 01 December 2010 - 03:40 AM, said:

Ok, tried to make the example above more generic. Can you try again?


because the problem an id is read only, then i changed variable id to _id like this:
$identity->_id=$id;


the complete code is like this:
public static function createAuthenticatedIdentity($id) {
    	$identity=new self($id,'');
    	$identity->_id=$id;
    	$identity->errorCode=self::ERROR_NONE;
    	return $identity;
}


now it works.
thanks for your clue mike..
you're great..
0

#17 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 03 December 2010 - 03:44 AM

Take a look at the fixed example above. You should not have to set _id manually anymore.
0

#18 User is offline   chirvo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 72
  • Joined: 30-November 10
  • Location:Venezuela

Posted 03 December 2010 - 08:23 PM

In the link below I found a different approach. It is definetly cleaner:

http://www.redbookme...login-with-yii/

Here I paste the code:

// UserIdentity is your extended class of CUserIdentity
$identity=new UserIdentity($userId, $pwd);
$identity->authenticate();
if ($identity->errorCode == UserIdentity::ERROR_NONE)
{
	$duration= 3600*24*30; // 30 days
	Yii::app()->user->login($identity,$duration);
	$this->redirect("your destination");
}
else
{
	// went south, // error processing
}

0

#19 User is offline   Thomas Jensen 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 01-August 09

Posted 05 January 2011 - 07:24 AM

chirvo, I don't see how that solves the problem? You've justed pasted the code for a standard login procedure?
0

#20 User is offline   Deepak Pradhan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 209
  • Joined: 01-May 09
  • Location:Charlotte, NC

Posted 05 January 2011 - 07:31 AM

Here is what I am using


	$identity = new UserIdentity($username, $password);
	$identity->authenticate();
	Yii::app()->user->login($identity, $duration = 0); // Do not remember Auto login user

0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users