0

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?

0

1 Answer 1

1

You can overwrite the register method from the RegistersUsers trait found here:

\Illuminate\Foundation\Auth\RegistersUsers

use the method below in your Auth controller and call your custom code in there:

/**
     * Handle a registration request for the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function register(Request $request)
    {
        $validator = $this->validator($request->all());

        if ($validator->fails()) {
            $this->throwValidationException(
                $request, $validator
            );
        }

        Auth::guard($this->getGuard())->login($this->create($request->all()));

        return redirect($this->redirectPath());
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. This works. One followup question. I have some other code in authenticated() that assigns a different session variable called shortName: $id = \Auth::user()->inst_id; $institution = \App\Institution::find($id); session(['shortName' => $institution->shortname]); This code works fine without being placed in the register function--it allows me to access shortName in the loaded page after registration. Do you know why I can assign the shortName session variable in authenticated() but not the loggedIn session variable for registration?
@Atlas Not sure without looking at the code, try wrapping the $institution = \App\Institution::find($id) part in a try catch block with \App\Institution::findOrFail($id) and catch \Illuminate\Database\Eloquent\ModelNotFoundException

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.