Laravel 我只能访问我的身份验证()->;方法中的user(),其中I auth()->;login()而不是同一控制器中的其他方法

Laravel 我只能访问我的身份验证()->;方法中的user(),其中I auth()->;login()而不是同一控制器中的其他方法,laravel,Laravel,我正在尝试在一个新的控制器中使用passport实现我自己的登录/注销 class AuthController extends AccessTokenController { use AuthenticatesUsers; . . 我的登录方法工作正常: public function login(ServerRequestInterface $request) { if (!auth()->attempt([ 'email' =

我正在尝试在一个新的控制器中使用passport实现我自己的登录/注销

class AuthController extends AccessTokenController
{
    use AuthenticatesUsers;
.
.
我的登录方法工作正常:

public function login(ServerRequestInterface $request)
    {
        if (!auth()->attempt([
            'email' => $request->getParsedBody()['email'],
            'password' => $request->getParsedBody()['password']
        ])) {
            return response()->json('failed attempt...');
        }
        auth()->login(User::where('id', Auth::user()->id)->first());
        .
        .
        // I can access auth()->user() here just fine ..
    }
但是我无法在注销方法中访问经过身份验证的用户,因此我可以获取他的令牌并将其删除

public function logout()
    {
        //I can't access the authenticated user here
        return auth()->user();

        //return response()->json('Logged out successfully', 200);
    }
我做错了什么? 注意:我在登录方法中遗漏了与颁发令牌相关的任何内容,因为它与问题无关。

更新:my routes/api.php

Route::post('register', 'Auth\RegisterController@register');
Route::post('login', 'Auth\AuthController@login');
Route::post('logout', 'Auth\AuthController@logout');

如果您使用的是
api
,则应发送
授权标头
,否则它应适用于
会话
基于
的身份验证

然后,您可以使用请求访问经过身份验证的用户

public function logout(Request $request)
{
  return $request->user(); //the user that made the request (the authenticated user)
}
或:

public function logout(Request $request)
{
   return Auth::user(); //the user that made the request (the authenticated user)
}

您正在发送授权标头吗?尝试转储您的标题请添加您的路由/api。php@SalmanZafar不,我没有在我的头中发送注销请求的授权,但我不认为这是阻止我访问经过身份验证的用户的原因。@Lotfi如果您使用的是api,那么您应该发送授权头,否则它应该适用于基于会话的应用程序authenticated@SalmanZafar谢谢,这就解决了。我在标题中发送了授权,然后通过`return$request->user()访问用户`