Why not just set your session driver to redis rather than explicitly setting the session?
Store user session in Redis
Hello there,
What i'm trying to accomplish is when a user logs in to store the SessionId in Redis.
Everything works well except for the fact that the SessionID is different. Let me show you some code
On \App\Listeners\UserEventSubscriber i have
public function onUserLogin($event)
{
Redis::set('session:'.Session::getId(), Auth::id());
}
public function subscribe($events)
{
$events->listen(
'Illuminate\Auth\Events\Login',
'App\Listeners\UserEventSubscriber@onUserLogin'
);
}
Laravel gives me the session just before regenerating it and that's why it's failing. How to get the right sessionid for the right user when a user logs in ?
Any help would be appreciated
Finally did it.
For those who are interested do this:
Overwrite the sendLoginResponse method in LoginController and put your code just after the regenerating of the session i.e.
/**
* Send the response after the user was authenticated.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
protected function sendLoginResponse(Request $request)
{
$request->session()->regenerate();
// Store the user session in Redis
Redis::set('session:'.Session::getId(), Auth::id());
$this->clearLoginAttempts($request);
return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($this->redirectPath());
}
Have fun and happy coding ! :)
Please or to participate in this conversation.