Currently I using Laravel 5.7 and trying to build login mechanism. Case similar to Custom user authentication base on the response of an API call.
My case is I don't have own database and user table locally. All need is to call API to validate by passing username, password, client id, client secret.
My request to API in postman:
POST Body
{
"username": "tester",
"password": "ps",
"CLIENT_ID": "xx",
"CLIENT_SECRET": "yy"
}
The response from API for success event in postman. The user information is in this JWT token by decode it.
{
"token_type": "Bearer",
"id_token": "eyJraWQiOiNGUvdFZC...",
"access_token": "eyJraWQiOi....",
"expire_in": 3600,
"refresh_token": "eyJjdHkiOiJK..."
}
I wish to do something like in loginContoller and use Auth::**:
public function postSignIn(Request $request)
{
$username = strtolower($request->username);
$password = $request->password;
if (Auth::attempt(['username' => $username, 'password' => $password])) {
return Redirect::to('/dashboard')->with('success', 'Hi '. $username .'! You have been successfully logged in.');
} else {
return Redirect::to('/')->with('error', 'Username/Password Wrong')->withInput(Request::except('password'))->with('username', $username);
}
}
Question:
How can I implement API authentication in laravel? (using guzzle, service provider, Authenticatable Contract and driver Auth?)
How to store the access token in session/cookie to attach in every request sent to API every time afterward?
How to store the refresh token and use it to gain the access token after it is invalid?
I appreciate all the helps or any example/guidance.