douyou8266 2018-10-13 01:38
浏览 113
已采纳

Laravel护照检查用户是否在登录前处于活动状态

I using Guzzle to send post request to passport oauth/token and here is the code:

public function login (Request $request) {
    $http = new \GuzzleHttp\Client;

    try {
        $response = $http->post(config('services.passport.login_endpoint'), [
            'form_params' => [
                'grant_type' => 'password',
                'client_id' => config('services.passport.client_id'),
                'client_secret' => config('services.passport.client_secret'),
                'username' => $request -> email,
                'password' => $request -> password,
            ],
        ]);
        return $response->getBody();
    } catch (\GuzzleHttp\Exception\BadResponseException $e) {
        if ($e->getCode() === 400)
            return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
        else if ($e->getCode() === 401)
            return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
        else if ($e->getCode() === 408)
            return response()->json('User is deactivated. Please try again', $e->getCode());

        return response()->json('Something went wrong on the server.', $e->getCode());
    }
}

What I am trying to do:

I have an active field for each user 1 is active and 0 is deactivated.

I want to check before the user login if active or not and send a response with 408 code and message like i did in the if statement.

What I have tried to do:

I have tried to add this to User.php model:

public function findForPassport($identifier) {
    return User::orWhere('email', $identifier)->where('active', 1)->first();
 }

It works fine.

Problem is:

1) This return a response with 401 if not active and I want to return 408.

2) I don't know if this is the best way to do it or not.

  • 写回答

3条回答 默认 最新

  • doubiao7267 2018-10-13 20:19
    关注

    I have found a good solution by changing the login function code to this:

    $http = new \GuzzleHttp\Client;
    
        // CHECK IF EMAIL IS ACTIVE
        $active = User::where('email', $request -> email) -> where('active', 0) -> exists();
        if($active)
            return response()->json('This user is not active', 403);
    
        try {
            $response = $http->post(config('services.passport.login_endpoint'), [
                'form_params' => [
                    'grant_type' => 'password',
                    'client_id' => config('services.passport.client_id'),
                    'client_secret' => config('services.passport.client_secret'),
                    'username' => $request -> email,
                    'password' => $request -> password,
                ],
            ]);
            return $response->getBody();
        } catch (\GuzzleHttp\Exception\BadResponseException $e) {
            if ($e->getCode() === 400)
                return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
            else if ($e->getCode() === 401)
                return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
            return response()->json('Something went wrong on the server.', $e->getCode());
        }
    

    This way I could simply check if the user is active or not using the email.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题