How to invalidate all tokens for an user in laravel passport

0 votes

In our app when user logs out we invalidate the access token for that particular device this way.

$user = $request->user();

$value = $request->bearerToken();
$id = (new Parser())->parse($value)->getHeader('jti');
$token = $user->tokens->find($id);
$token->revoke();

But when an user deactivates his/her account, we would like to invalidate all the access tokens from all the devices the user is logged in. I looked through the document but did not find anything useful.

Oct 23, 2020 in Laravel by kartik
• 37,510 points
4,313 views

1 answer to this question.

0 votes

Hello @kartik,

One of the methods it provides is tokens(), which defines a hasMany relationship between Laravel\Passport\Token and models using the trait. You can use this to retrieve a list of all of the tokens for a given user:

$userTokens = $userInstance->tokens;

The token model itself has a revoke method:

foreach($userTokens as $token) {
    $token->revoke();   
}

Hope it helps!!

answered Oct 23, 2020 by Niroj
• 82,880 points

Related Questions In Laravel

0 votes
1 answer

How to set port for php artisan.php serve in Laravel?

Hii @kartik, When we use the php artisan serve ...READ MORE

answered Mar 30, 2020 in Laravel by Niroj
• 82,880 points
16,175 views
0 votes
1 answer

How to get distinct values for non-key column fields in Laravel?

Hello @kartik In eloquent you can use this $users ...READ MORE

answered Mar 30, 2020 in Laravel by Niroj
• 82,880 points
22,008 views
0 votes
1 answer

How to install all required PHP extensions for Laravel?

Hello, Laravel Server Requirements mentions that BCMath, Ctype, JSON, Mbstring, OpenSSL, PDO, Tokenizer and XML extensions are required. Most the ...READ MORE

answered Apr 6, 2020 in Laravel by Niroj
• 82,880 points
21,632 views
0 votes
1 answer

How to get all pending jobs in laravel queue on redis?

Hello @kartik, Here is the way I do ...READ MORE

answered Apr 6, 2020 in Laravel by Niroj
• 82,880 points
8,882 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,880 points
3,711 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,880 points
2,919 views
0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,880 points
830 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,880 points
772 views
0 votes
1 answer

How to get all the users except current logged in user in laravel eloquent?

Hello @kartik, You can get the current user's ...READ MORE

answered Dec 8, 2020 in Laravel by Niroj
• 82,880 points
6,826 views
0 votes
1 answer

.How to turn off CSRF protection for a particular route in Laravel?

Hey, We can add that particular URL or ...READ MORE

answered Mar 24, 2020 in Laravel by Niroj
• 82,880 points
3,891 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP