I'm currently using 2 projects. 1 front end (with laravel backend to communicate with API) and another laravel project (the API).

现在,我使用Laravel Passport对用户进行身份验证,并确保每个API调用都是经过授权的调用.

现在,当我想要注销我的用户时,我向我的API发送POST请求(使用持有者令牌),并try 将他从API注销(并清除会话、Cookie.)

然后,在客户机上,我还刷新了我的会话,这样令牌就不再是已知的.现在,当我返回登录页面时,它会自动登录我的用户.(或者我的用户仍在登录).

Can someone explain me how to properly log out a user with Laravel passport?

Thanks in advance.

推荐答案

您需要从数据库表oauth_access_tokens中删除令牌

  1. Run the command php artisan make:model OauthAccessToken to create the model.

  2. 然后创建User型号和新创建的OauthAccessToken型号之间的关系,在User.php中添加:

    public function AauthAcessToken(){
        return $this->hasMany('\App\OauthAccessToken');
    }
    
  3. 在用户控制器中.php,创建一个新的注销函数:

    public function logoutApi()
    { 
        if (Auth::check()) {
           Auth::user()->AauthAcessToken()->delete();
        }
    }
    
  4. 在api中.php路由,创建新路由:

     Route::post('logout','UserController@logoutApi');
    
  5. Now you can logout by calling posting to URL /api/logout

Laravel相关问答推荐

无法在终端cPanel中打开输入文件:artisan

如何定制有关密码被泄露的Laravel Breeze S消息?

Livewire 基于下拉 Select 自动填充输入框

LARAVEL 从多个表中获取重复的行

如何通过 Eloquent 获取单个集合中每位教师的学生人数

如何在自定义表单请求中获取错误验证数据

在 Laravel 包中的路由上使用显式或隐式模型绑定

如何在laravel中输出奇偶行

laravel 基本路径

Laravel:如何在没有数据库的情况下对用户进行身份验证

条带 api 判断现有卡

Laravel 5.1 重命名项目

如果值存在于另一个字段数组中,Laravel 验证规则

PHP Laravel:如何获取客户端浏览器/设备?

Laravel 5 干预中的图像验证

Laravel 项目旁边的 Wordpress 项目(在 public_html 文件夹中)

Laravel Socialite 在本地主机上进行测试,SSL 证书问题?

Laravel 4:将什么作为参数传递给 Url 类?

Laravel 如何从子域 URL 中删除api前缀

Laravel 单元测试依赖注入