JWT 身份验证 user_not_found Tymon

JWT Authentication user_not_found Tymon

我已经为 JWT 身份验证设置了 Tymon 包。如果是新用户注册或登录,我会成功获得令牌。但是当我将令牌传递给 Laravel JWT 时,我收到一个错误,因为找不到用户。

控制器代码

public function authenticate()
    {
        $credentials = request()->only('user_name','password');
        try{
            $token = JWTAuth::attempt($credentials);
            if(!$token){
                return response()->json(['error'=>'invalid_credentials'],401);
            }
        }
        catch(JWTException $e){
            return response()->json(['error'=>'something went wrong'],500);
        }
        return response()->json(['token'=>$token],200);
    }

    public function register()
    {
        $user_name = request()->user_name;
        $c_name = request()->company_name;
        $accessibility_level = request()->accessability_level;
        $password = request()->password;
        $contact_number = request()->contact_number;
        $address = request()->address;

        $user = User::create([
            'user_name'=>$user_name,
            'c_name'=>$c_name,
            'accessibility_level'=>$accessibility_level,
            'password'=>bcrypt($password),
            'contact_number'=>$contact_number,
            'address'=>$address
        ]);

        $token = JWTAuth::fromUser($user);

        return response()->json(['token'=>$token],200);
    }

上面的代码没问题。

但是,当我尝试使用 JWT 验证访问某些数据时,出现 USER_NOT_FOUND 错误。我已经通过 Postman 传递了我作为 header 获得的令牌。

路线代码

Route::get('/some_route','some_controller@index')->middleware('jwt.auth');

并且 jwt.php 也设置了我在模型中使用的正确标识符(主键)

'identifier' => 'user_name',

JWT 标识符无法通过简单地修改配置来工作,因为出于某种原因它 hardcoded 作为代码中的 id

您当然可以在调用任何其他 JWTAuth 方法之前使用 setIdentifier 方法来设置标识符。

方法如下:

public function authenticate()
    {
        $credentials = request()->only('user_name','password');
        try{
            $token = JWTAuth::setIdentifier('user_name')->attempt($credentials);
            if(!$token){
                return response()->json(['error'=>'invalid_credentials'],401);
            }
        }
        catch(JWTException $e){
            return response()->json(['error'=>'something went wrong'],500);
        }
        return response()->json(['token'=>$token],200);
    }

然后为 jwt 身份验证创建自定义中间件:

public function handle($request, \Closure $next)
    {
        if (! $token = $this->auth->setIdentifier('user_name')->setRequest($request)->getToken()) {
            return $this->respond('tymon.jwt.absent', 'token_not_provided', 400);
        }

        try {
            $user = $this->auth->authenticate($token);
        } catch (TokenExpiredException $e) {
            return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]);
        } catch (JWTException $e) {
            return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]);
        }

        if (! $user) {
            return $this->respond('tymon.jwt.user_not_found', 'user_not_found', 404);
        }

        $this->events->fire('tymon.jwt.valid', $user);

        return $next($request);
    }