Jump to content

user->isLoggedin() lags behind $session->login()


Martin Muzatko
 Share

Recommended Posts

Hello there!

I want to create a user front-end (user can register/login/logout via templates)

I'm working based on the intermediate site profile. So _init.php is loaded first, then the template file and then _main.php.

I integrated the custom login as described here, and changed it to my needs. ( I don't want to redirect the user, if the form is filled in successfully)

The problem I face, is that $user->isLoggedin() lags behind $session->login().

Which means that whenever I do a login, I DO get the information that the login was successful ($session->login(...) instanceof User). I COULD use that information on _main.php to show a profile in the upper right corner.

However I don't want to set a variable in the template and ask for it in _main.php. Are there any alternatives? Is a redirect really required to complete the session handling? Why? I have the same problem for the logout. The user is still displayed as logged in, when he opens the logout page.

Thanks in advance.

Best,
Martin

Link to comment
Share on other sites

Can you post your entire code (or at least the relevant bits)?

3 hours ago, Martin Muzatko said:

Is a redirect really required to complete the session handling? Why?

That's simply how sessions work. You can't set new session vars and retrieve them in one go.

From http://php.net/manual/en/features.sessions.php

Quote

Session support in PHP consists of a way to preserve certain data across subsequent accesses.

The keyword being "subsequent" :-)

 

  • Like 2
Link to comment
Share on other sites

Hello. Thanks for your reply.

I know that sessions are supposed to work like that. I only wonder why the $user variable is only populated after a complete refresh. After all, after a successfull $session->login, I would expect $user->isLoggedIn() to return true. Even without a page refresh.

 

I wanted to display a little welcome message, or logout message, but I think I can also achieve this by redirecting him to another page. 

This is my current code - with the redirect in place.

 

<?php namespace ProcessWire;
    if (count($input->post()->getArray())) {
        $loginUser = $session->login($input->post->username, $input->post->password);
        if($loginUser instanceof User) {
            $session->redirect($pages->get('/')->url.'users/'.$loginUser->name);
        }
    }
    if($user->isLoggedin()) {
        $session->redirect($pages->get('/')->url.'users/'.$user->name);
    }
?>
<form action="" method="POST">
    <input type="text" name="username" autocomplete autofocus value="<?=$input->post->username?>">
    <input type="password" name="password" autocomplete value="">
    <input type="submit" value="Login">
</form>

 

Link to comment
Share on other sites

After verifying the login, you can do

$users->setCurrentUser($loginUser);

Afterwards, $user->isLoggedin() etc. should behave as expected. Setting the user "guest" as the current user (that part hasn't been tested by me) should accomplish the same after a logout.

  • Like 3
Link to comment
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
 Share

×
×
  • Create New...