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条)

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致