2

I have a scenario where I am putting certain information in session after user is successfully logged in. I am using Laravel 5.2. As in Laravel 5.2 you just redirect user to /login and specify protected $redirectTo variable and the rest is cared by Laravel itself. Now I want whenever user is logged in successfully, I want to put some information in session. Currently what I am doing when user is redirected to my protected $redirectTo url I put information in that like

\Session::put('user.data', $someData);

It works fine in normal scenario. But say user is logged out and tries to access some auth protected url, say example.com/showUsers saved in his browser. Now Laravel will check its a login protected url it will redirect user to login page, user is successfully logged in and Now in this case Laravel redirects the user to previous url. Now the information that I want to put in session isn't in session so I get error.

I have checked Auth/AuthController, Authication.php in middleware but don't find any place.

Currently I am putting it in /app/Http/Middleware/Authenticate.php before the last statement return $next($request); like this

if(empty(\Session::get('user.data'))) {
        $someData = getSomeData();
        \Session::put('user.agency', $someData);
}
return $next($request);

I don't know its a good approach or not So can any body let me know am I doing good or where do I put my session code that is executed every time user logs in.

1 Answer 1

2

As far as I understood, you are using the AuthControllerand AuthenticatesUsers trait to log your users in which is the default of Laravel.

In AuthenticatesUser trait on line 114, you will see a method definition check which was written for the users of the framework to define additional login related work.

/**
 * Send the response after the user was authenticated.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  bool  $throttles
 * @return \Illuminate\Http\Response
 */
protected function handleUserWasAuthenticated(Request $request, $throttles)
{
    if ($throttles) {
        $this->clearLoginAttempts($request);
    }
    if (method_exists($this, 'authenticated')) {
        return $this->authenticated($request, Auth::guard($this->getGuard())->user());
    }
    return redirect()->intended($this->redirectPath());
}

In your AuthController class,

creating a method called authenticated and doing your work there should be a more expected behaviour.

/**
 * Return the authenticated response.
 *
 * @param  $request
 * @param  $user
 * @return \Illuminate\Contracts\Routing\ResponseFactory
 */
protected function authenticated(Request $request, $user)
{
    // Here.
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for that. Thats exactly what I was looking for. One more question, If I need to override registration process of Laravel, what are the files I will be looking at?

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.