Laravel 7 Sanctum注销 [英] Laravel 7 Sanctum logout

查看:52
本文介绍了Laravel 7 Sanctum注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序使用具有Sanctum身份验证的Laravel7.
如何执行注销过程?
我使用:

I'm using Laravel 7 with Sanctum authentication for my app.
How can i implement the logout procedure?
I use:

Auth::user()->tokens()->delete();

,它可以工作,但是会删除该用户的所有令牌.我只想删除请求注销的用户的令牌,这样其他会话应该保持打开状态

and it works, but It delete all tokens of this user. i would like to delete only the token of the user who requested the logout, in this way the other sessions should remain open

推荐答案

您需要指定用户:

// Revoke a specific user token
Auth::user()->tokens()->where('id', $id)->delete();

// Get user who requested the logout
$user = request()->user(); //or Auth::user()
// Revoke current user token
$user->tokens()->where('id', $user->currentAccessToken()->id)->delete();

这篇关于Laravel 7 Sanctum注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆