mercredi 30 mars 2016

Using Laravel 5.2's Authenticated() Method

I am using Laravel 5.2's default authentication and having some trouble with the behavior of the framework's authenticated() method. The method is useful for running code after successful authentication occurs but before loading any subsequent pages. A successful authentication will trigger the authenticated() method in AuthController, which I have defined as follows:

protected function authenticated() {
        session(['loggedIn' => 'show']);
        return redirect('/home');
}

As we see, it can be useful for setting session variables that are required when the homepage first loads up (but that should not be reset each time the homepage is reloaded).

In my case, I use loggedIn to display a certain welcome div only once per session after the user logs in or registers. I include the following PHP on the homepage for that:

function displayWelcome() {
    if (session('loggedIn') == 'show') {
        echo '<div class="container" name="loggedIn" id="loggedIn">';
        session(['loggedIn' => 'hide']);
    } else {
        echo '<div class="container" name="loggedIn" id="loggedIn" hidden>';
    }
}

Presently, this code works fine when existing users log in.

It does not, however, fully work for user registrations. It does successfully redirect to the homepage, but the welcome div never shows up at all. Oddly enough, when I echo session('loggedIn') before calling the displayWelcome() function, it outputs "hide" after registration (and correctly displays "show" after login). I fail to see how it is acquiring the value "hide" and why it is not correctly opening the div.

Any hints?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire