user(); assert($user !== null); // Sanctum's own docblock types this as non-nullable, but the // underlying property is simply unset when the request was not // token-authenticated. Narrowing it here keeps the null branch // honest instead of letting the analyser delete it. /** @var PersonalAccessToken|null $token */ $token = $user->currentAccessToken(); if ($token !== null) { // `via` and `api_token` are stamped by ActivityLogger itself. $this->activity->log(Action::ApiTokenRevoked, $user, context: ['token_name' => $token->name]); $token->delete(); } return response()->json(status: 204); } }