Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stemount/563a191a08398c03b3a07ae1bce2b4ae to your computer and use it in GitHub Desktop.
Save stemount/563a191a08398c03b3a07ae1bce2b4ae to your computer and use it in GitHub Desktop.
Force/Revoke All Laravel User Sessions and Passport Tokens
string(65) "=== Forcibly logging out xxxxxxxx-xxxxxxxx-xxxxxxxx-xxxxxxxx-xxxxxxxx ==="
string(325) "revoking user token {"id":"xxxxxxxx","user_id":"xxxxxxxx","client_id":215,"name":"UserToken","scopes":[],"revoked":true,"created_at":"2020-01-03 11:53:21","updated_at":"2020-01-03 11:53:21","expires_at":"2040-01-03 11:53:21"}"
string(68) "=== Forcibly logged out out xxxxxxxx-xxxxxxxx-4646-9854-xxxxxxxx ==="
<?php
// THIS SCRIPT IS 2 MIN HACK THAT FORCIBLY REVOKES ALL LARAVEL USER SESSION/DATA AND LARAVEL PASSPORT TOKENS.
// YOUR USERS WILL ALL BE LOGGED OUT WHICH MAY PISS THEM OFF.
//
// USE AT YOUR OWN RISK.
// This would probably run out of RAM if you have a load of users.
// Refactor to do chunking if you have loads of users.
$users = User::all();
foreach ($users as $user) {
var_dump("=== Forcibly logging out {$user->id} ===");
Auth::setUser($user);
Auth::logout();
/* @var \Illuminate\Database\Eloquent\Collection|\Laravel\Passport\Token $tokens */
$tokens = $user->tokens ?? false;
if ($tokens) {
foreach ($tokens as $key => $token) {
$token->revoke();
var_dump("revoking user token {$token}");
}
}
var_dump("=== Forcibly logged out out {$user->id} ===");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment