Jump to content
Harmster

$users->setCurrentUser() does not give desired result

Recommended Posts

Hey

I'm trying to log a user in with a cookie. cookie stuff works fine as far as I can tell but logging the user in without a password gives me a bucket of problems.

$u = $users->get($user_id);
set_remember_me($u->name, $wire);
echo "<br />Username retrieved from: ".$u->name;
$users->setCurrentUser($u); 
$session->_user_id = $u->id;  
echo "<br />After setting current user: ".$user->name
$remember->trash();

It echoes the following:

Username retrieved from: Harmster
After setting current user: guest

What am i doing wrong, or what is the correct way to do this?

Share this post


Link to post
Share on other sites

$user isn't updated since you're in a template and $user was set before you execute this code. 

$users->setCurrentUser($u);
echo "<br />After setting current user: " . wire("user")->name

So the new current user would only be available via wire("user") or $users->getCurrentUser(), that will get the new current user.

  • Like 2

Share this post


Link to post
Share on other sites

$user isn't updated since you're in a template and $user was set before you execute this code. 

$users->setCurrentUser($u);
echo "<br />After setting current user: " . wire("user")->name

So the new current user would only be available via wire("user") or $users->getCurrentUser(), that will get the new current user.

So would this make sense then Soma?

// $user is initially the user at time of page load
 
// ... some code
 
$users->setCurrentUser($u);
$user = $users->getCurrentUser();
 
// ... carry on using $user as normal

Unless maybe you could do $user = $users->setCurrentUser($u) but I don't know as I've not tested any of this - just thinking out loud.

  • Like 2

Share this post


Link to post
Share on other sites

So would this make sense then Soma?

// $user is initially the user at time of page load
 
// ... some code
 
$users->setCurrentUser($u);
$user = $users->getCurrentUser();
 
// ... carry on using $user as normal

Unless maybe you could do $user = $users->setCurrentUser($u) but I don't know as I've not tested any of this - just thinking out loud.

EDIT: Didnt read... Sorry :S

And it does work 

  • Like 3

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...