skip to Main Content

I searched but could not find a way to set a timeout for the active session.
I’m using Laravel 10 and using the cookie session driver since there are multiple servers. I want to control the session time with by 2 factors:

  1. A session timeout if the user is inactive for 30 minutes. For this
    I’m setting SESSION_LIFETIME in the .env to 30
  2. A session timeout of 1 hour irrespective of the user being active or inactive

I do not have any idea if there is any setting to manage the second condition as well or I need to manage it myself by storing the logged in time and checking it on every request.
Any help, hint or idea is appreciated.

2

Answers


  1. In config/session.php you can add:

    'lifetime' => 30 // determines how long a session will remain active after the last activity.
    

    And set the cookie:

    'cookie' => [
        'lifetime' => 60 // determines the time the session is active regardless if the user closes the browser or not
    
    Login or Signup to reply.
  2. Yes, you can achieve this using middleware.
    Something like that:

       $user = $request->user();
          if ($user && $user->last_login_at) {
            $sessionTimeout = Carbon::parse($user->last_login_at)->addHour(); 
            if (Carbon::now()->gt($sessionTimeout)) {
                auth()->logout();
                return redirect()->route('login');
            }
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search