如何在所有页面中使用生成的JWT令牌进行身份验证

时间:2017-03-23 06:00:13

标签: php mysql laravel-5.4

我使用JWT身份验证生成令牌。接下来我该如何在所有页面中进行身份验证,例如,如果我还没有登录,如果移动到其他页面,那么它应该对登录进行身份验证。生成JWT身份验证令牌后,请告诉我接下来应该做什么。

这是我生成JWT令牌的代码

public function authenticate(Request $request)
{
    //$userinfo=DB::connection('mysqll')->table('register')->select('_id','fname','lname','username','password','email','mobileno')->get();
    $userinfo=DB::connection('mysql')->table('register')->select('id','fname','lname','username','password','email','mobileno','created_at')->first();

    //$credentials = JWTAuth::fromUser($userinfo);

    // grab credentials from the request
    //$credentials = $request->only('name','password');

    try {

        //$token = JWTAuth::attempt($credentials);
        //dd($token);
        // attempt to verify the credentials and create a token for the user
        if (!$token = JWTAuth::fromUser($userinfo)) {
            return response()->json(['error' => 'invalid_credentials'], 401);
        }
    } catch (JWTException $e) {
        // something went wrong whilst attempting to encode the token
        return response()->json(['error' => 'could_not_create_token'], 500);
    }

    // all good so return the token
    return response()->json(compact('token'));
}

令牌是

{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6XC9cL2xvY2FsaG9zdDo4MDAwXC9hcGlcL2F1dGhlbnRpY2F0ZSIsImlhdCI6MTQ5MDE4NTYxNSwiZXhwIjoxNDkwMTg5MjE1LCJuYmYiOjE0OTAxODU2MTUsImp0aSI6ImJiOTZjNzQyNzVhOTliYmQxM2I1ZjQxZDcwMGIwMGE2In0.hwqnwtRSOLGSMhHJMufFKdp7CPz6nlhhiVL5sePIDZw"}

如果我移动到没有登录的其他页面,那么下一步要进行身份验证的步骤是什么。

0 个答案:

没有答案