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.