【问题标题】:how to create a token in laravel tymons/jwt-auth如何在 laravel tymons/jwt-auth 中创建令牌
【发布时间】:2020-04-20 21:02:17
【问题描述】:

我想创建一个使用用户角色编码的令牌。我试过查看文档,但我没有得到令牌。我已经尝试过。

我使用的是 laravel 5.8 和包版本 "tymon/jwt-auth": "^1.0.0-rc.2"

谢谢

授权控制器

 public function login()
    {
        $credentials = request(['email', 'password']);

        if (! $token = auth()->guard('api')->attempt($credentials)) {
            return response()->json(['errors' => 'In-valid username and Password'], 401);
        }

        $customClaims =[
           'role' => auth('api')->user()->getRoleNames()
        ];
        $payload = JWTFactory::make($customClaims); 
        $token = JWTAuth::encode($payload);

        return $this->respondWithToken($token);
    }

  protected function respondWithToken($token)
    {
        return response()->json([  
            'success' => true, 
            'access_token' => $token,
            'token_type' => 'bearer',
        ]);
    }  

【问题讨论】:

    标签: laravel


    【解决方案1】:

    你的用户模型应该是这样的

    class User extends Authenticatable implements JWTSubject
    {
        use Notifiable, HasRoles;
    
        public function getJWTIdentifier()
        {
            return $this->getKey();
        }
        public function getJWTCustomClaims()
        {
            return [];
        }
    
    
    }
    
    
    
    
    public function login()
        {
            $credentials = request(['email', 'password']);
    
            if (!$token = JWTAuth::attempt($credentials)) {
                return response()->json(['errors' => 'In-valid username and Password'], 401);
            }
    
            $customClaims =[
               'role' => auth('api')->user()->getRoleNames()
            ];
            $payload = JWTFactory::make($customClaims); 
            $token = JWTAuth::encode($payload);
    
            return $this->respondWithToken($token);
        }
    

    【讨论】:

    • 我改了答案
    【解决方案2】:

    基于documentation,您可能需要执行两次attempt(),如下所示:

    public function login()
    {
        $credentials = request(['email', 'password']);
    
        if (!auth()->guard('api')->claims(['role' => 'bar'])->attempt($credentials)) {
            return response()->json(['errors' => 'In-valid username and Password'], 401);
        }
    
        $token = auth('api')->claims(['role' => auth('api')->user()->getRoleNames()])->attempt($credentials);
    
        return $this->respondWithToken($token);
    }
    

    【讨论】:

    • 我正在获取令牌
    • 非常感谢,我有一个疑问如何解码和检查令牌?我是否得到了这个角色?
    • @matheenulla 查看this。您应该能够使用auth('api')->payload() 查看有效负载
    【解决方案3】:

    试试这个

    use JWTAuth;
    use Tymon\JWTAuth\Exceptions\JWTException;
    
    class AuthenticateController extends Controller
    {
        public function login(Request $request)
        {
            // grab credentials from the request
            $credentials = $request->only('email', 'password');
    
            try {
                // attempt to verify the credentials and create a token for the user
                if (!auth()->guard('api')->claims(['role' => 'bar'])->attempt($credentials)) 
                    {
                     return response()->json(['errors' => 'In-valid username and Password'], 401);
                    }
    
                 $token = auth('api')->claims(['role' => auth('api')->user()->getRoleNames()])->attempt($credentials);
    
                 return $this->respondWithToken($token);
            } 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'));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-03-20
      • 2016-10-20
      • 2016-07-18
      • 2021-07-28
      • 1970-01-01
      • 2018-10-21
      • 2020-05-11
      • 2017-01-06
      • 2018-11-28
      相关资源
      最近更新 更多