Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force logout of all users in Laravel

How can I force all users to be logged out in a SPA? I want it so that when I deploy a new version, all users automatically get logged out.

I tried the following, but I'm not sure if it's the safest way to do it.

php artisan key:generate
like image 930
nikospap Avatar asked Nov 28 '18 20:11

nikospap


2 Answers

If your session data is stored in the database, you need to clear the sessions table. Running a simple SQL query will solve the problem:

DELETE FROM sessions;

If you sessions are stored in files, then as @Karl suggests, you need to delete the session files from the filesystem:

rm -rf storage/framework/sessions/*

The name of the session cookie can also be changed to force all sessions to be invalid, but this is a code change rather than clearing data. The name can be updated in the cookie key in config/session.php file. This option is NOT recommended.

like image 61
Niraj Shah Avatar answered Oct 09 '22 20:10

Niraj Shah


You can destroy all the sessions. If you use Laravel Envoy to handle deployments, you can add the following line.

rm -rf storage/framework/sessions/*

If you're using the database session driver, clearing the sessions table is easy.

DB::table('sessions')->truncate();
like image 27
Karl Hill Avatar answered Oct 09 '22 21:10

Karl Hill