Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

unixlab's avatar

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

0 likes
9 replies
usama.ashraf's avatar

Why not just set your session driver to redis rather than explicitly setting the session?

usama.ashraf's avatar

Just set this in your .env SESSION_DRIVER=redis and you can use sessions just like you would otherwise. And you don't have to explicitly set the user login session. Laravel takes care of that.

unixlab's avatar

I need the database driver too. I'm storing the sessions in Redis because i have node.js server which authenticates users based on their laravel cookie (which holds the sessionid)

usama.ashraf's avatar

Ok. Your problem might be related to cookies/sessions being encrypted.

But a better approach would be to use JWT (JSON Web Tokens). Look them up.

usama.ashraf's avatar

On login, the user will be provided a JWT that will have to be sent to the node server. Your node server could then decode the JWT to retrieve the user. Or it'll fail in case of an invalid token.

unixlab's avatar

No. When a user logs in laravel fires an event for that and gives the user object.

On that exact moment i have access to the current sessionid which after the event is fired is being regenerated.

Here lies the problem.

My node.js authentication system is based on sessions and that's why i don't want to use JWT

bugsysha's avatar

I need the database driver too. I'm storing the sessions in Redis because i have node.js server which authenticates users based on their laravel cookie (which holds the sessionid)

Also interested in solution...

unixlab's avatar
unixlab
OP
Best Answer
Level 4

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.