Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger function after session timeout or expire in laravel

Hello i'm kinda new to laravel and i have a question concerning authentication. I have the following function in my authentication controller:

public function signout()
    { 
        // set logged in status to zero in database
        $l = Login::where('user_id', Session::get('user')->user_id)
                ->where('logged_in', 1)->first(); 
        $l->logged_in = 0;
        if ($l->save())
        {
            // log user out
            Auth::logout();
            // Forget user session data
            Session::forget('user');
            // redirect user to login page
            return Redirect::to('/account/signin');
        }
    }

Now in my session config, i have set sessions to expire after 60mins after which the user will obviously be logged out of the system. However that will occur without my other functions executing like setting user logged in status to zero in database or forgetting the user session array. Is there a way i can trigger those functions to execute after login session expire? Thank you in advance.

Update: I've been looking around again ever since i got a down vote for my question to see if there was already a solution to this, from reading the docs i got excited when i came to the "Events" section because i thought i had found a solution however i found out later on that there was no such thing as a "Session::expire" event in laravel, neither is there a function to check whether another user is logged in or not.

like image 853
user3718908x100 Avatar asked Feb 22 '15 16:02

user3718908x100


People also ask

How does laravel handle session timeout?

'expire_on_close' => true, that decides if session will be expired when browser will be closed. Those are default values. The first one means how long session cookie will be stored - default value is 0 (until browse is closed).

How do I change session timeout in laravel?

If you want to increase your session life time then we need to change in . env file and it is very easy to change it from configuration file in laravel. laravel provides you session. php file there is we can see 'lifetime' key option for setting time in minutes.

Do session ids expire?

Answer. Yes the Session cookie expires. In addition to the 30 minute default timeout (if the visitor is idle for 30 minutes) the 'Session ID' cookie will expire at the end of an internet browser session.


1 Answers

Your whole premise is wrong: sessions should have an expiry timestamp that's set when user logs in, and updated on every request if you want to have something like "session times out after 1h of inactivity".

Then you can basically:

  1. Check if session is still valid when user performs a request, by checking the timestamp
  2. Delete expired sessions using a scheduled task, so you keep things clean and tidy in the background

Anyway, if for some reason you end up needing to trigger some actions to happen when a user signs out Laravel actually has an Event that's triggered on user logout: 'auth.logout'

like image 143
Alberto Avatar answered Oct 20 '22 06:10

Alberto