How to logout a user from API using laravel Passport

后端 未结 8 889
夕颜
夕颜 2020-12-12 16:38

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

Now I use Laravel Passport to auth

8条回答
  •  庸人自扰
    2020-12-12 16:51

    You need to delete the token from the database table oauth_access_tokens you can do that by creating a new model like OauthAccessToken

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

    2. Then create a relation between the User model and the new created OauthAccessToken Model , in User.php add :

      public function AauthAcessToken(){
          return $this->hasMany('\App\OauthAccessToken');
      }
      
    3. in UserController.php , create a new function for logout:

      public function logoutApi()
      { 
          if (Auth::check()) {
             Auth::user()->AauthAcessToken()->delete();
          }
      }
      
    4. In api.php router , create new route :

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

提交回复
热议问题