Programmatically log in a user

Permalink
How can I programmatically log in a user - using code. I have their User and UserInfo object, their groups are set up I now just need to log them in.

I have tried using User::loginByUserID($userID) documented here,http://www.concrete5.org/documentation/developers/permissions/users... it does not work with or without a redirect after it.

How can this be done?

yolk
View Replies: View Best Answer
mkly replied on at Permalink Reply
mkly
No love with this?
//where $user is User object of the user to login
$u = new User;
$u->loginByUserID($user->getUserID());

Make sure you aren't doing
$u = new User;
$u->loginByUserID($u->getUserID());

As this is going to log in the current user not the other user.


Best Wishes,
Mike
http://mkly.io
yolk replied on at Permalink Best Answer Reply
yolk
In the register controller it turns out you need to call $u->logout() before I did that log in code.

With the call to logout it worked!
freakidz replied on at Permalink Reply
Hmm, doesn't seem to be working for me. What exactly did you do?

Thanks!
yolk replied on at Permalink Reply
yolk
If you pass the second param to getByID() as true, that will log in the user.

User::getByUserID($uID, true);
freakidz replied on at Permalink Reply
Very good. Thanks!
Cahueya replied on at Permalink Reply
Where would I need to add this to make this work?

Thank you :)
yolk replied on at Permalink Reply
yolk
Erm, I am not sure. That entirely depends upon the context of your web site.

But technically, it could go anywhere.
Cahueya replied on at Permalink Reply
Sorry, I am a code n00b :)

So, I have set up custom registration pages (multilingual site), using jbs "register pro" add-on, which is just overriding the core registration function/controller etc.

So within the package files, the code is very similar to the core registration controller.

What I want to achieve is:

User registers to site -> User is added to group. Now the user shall be redirected to another page where he can choose some other options (purchase products etc.)..

All the steps are set up, but the user needs to log in to arrive at the last step, so that the purchase can be connected to the user data. I would like to jump over that and just have it as a flowing process.

From this thread, it seems like I just need to put some lines of code into two places... well :-)

Thanks for the reply!
yolk replied on at Permalink Reply
yolk
It sounds like you're most of the way there to be honest!

Yes you just need to take the code from above and insert it into the register controller, once the user has been created.