如何在登录 Lumen 7.0 和 JWT 时检查用户状态

时间:2021-04-18 12:07:03

标签: laravel postman lumen

我想检查用户是否是学生。如果不是,请注销并返回消息。 我现在拥有的:登录

      $this->validate($request, [
                'username' => 'required|max:255',
                'password' => 'required',
                'alias' => 'required',
            ]);
    
            $login = $request->input('username');
            $field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
            $credentials = $request->only($field, 'password', 'alias');
 try {
            // attempt to verify the credentials and create a token for the user
            if (!$token = $this->jwt->attempt($credentials)) {

                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);
        }

        if ($this->guard()->user()->user_type != 'student') {
              $this->guard()->logout();
            return response()->json(['message' => 'Only student can use the app']);
        }
  return $this->respondWithToken($token);

但它不起作用并在响应中给我错误“无法从请求中解析令牌。(500内部服务器错误)” 如果我删除 $this->guard()->logout(); ,那么它的工作原理。 但是如果不允许用户,我需要删除生成的令牌。我可以像 user_type 一样检查 attempt() 中的 alias 但是我无法提供正确的原因为什么他不能登录。 如何向用户发送正确的消息?

0 个答案:

没有答案
相关问题