Call to a member function revoke() on null exception

Viewed 31

I am trying to create a logout function in laravel Passport.

public function logout(Request $request) {
        $user = Auth::user()->token();
        $user->revoke();
        $tokens =  $user->tokens->pluck('id');
        Token::whereIn('id', $tokens)
            ->update(['revoked'=> true]);
        
        RefreshToken::whereIn('access_token_id', $tokens)->update(['revoked' => true]);
    }

I got this exception on postman. Is there someone who can help me for this? How can I solve this

Call to a member function revoke() on null

problem

"message": "Call to a member function revoke() on null",
"exception": "Error",
"file": "/Users/123/Desktop/Projeler/Passaport Api/App/app/Http/Controllers/ApiAuth.php",
"line": 52,
1 Answers

You need to include the route inner the passport Auth middleware like below:

Route::group(['middleware' => ['auth:api']], function() {    
 Route::post('logout',   [YourNameController::class, 'logout']);
});

hopefully that is help you

Related