I've created a Laravel backend with an API. In addition, I added Laravel passport for authentication.
Right now I want to access the API with my ReactJS Frontend application and with a React-Native application. The ReactJS application is on the same server as the Laravel backend.
Now I'm looking for the best way to connect everything with the best security.
I've been looking around and checking tutorials, HowTos, ... for over a week now and I don't know what is best.
Right now my "setup" is, because of some threads and stuff, like this:
I checked the Laravel documentation and several HowTos and implemented the Password Grant Client in my application:
public function login(Request $request){
$http = new GuzzleHttp\Client;
$response = $http->post(env('APP_URL') . '/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => env('PASSWORD_CLIENT_ID'),
'client_secret' => env('PASSWORD_CLIENT_SECRET'),
'username' => $request->input('email'),
'password' => $request->input('password'),
'scope' => '',
],
]);
$responseJSON = json_decode($response->getBody(), true);
$output = array(
'access_token' => $responseJSON['access_token'],
'expires_in' => $responseJSON['expires_in']
);
return response($output)
->header('Content-Type', 'application/json')
->cookie('refreshToken', $responseJSON['refresh_token'], 14400, null, null, true, true);
}
and for refreshing the token:
public function tryRefresh(Request $request) {
$http = new GuzzleHttp\Client;
$refreshToken = $request->cookie('refreshToken');
$response = $http->post(env('APP_URL') . '/oauth/token', [
'form_params' => [
'grant_type' => 'refresh_token',
'refresh_token' => $refreshToken,
'client_id' => env('PASSWORD_CLIENT_ID'),
'client_secret' => env('PASSWORD_CLIENT_SECRET'),
'scope' => '',
],
]);
$responseJSON = json_decode($response->getBody(), true);
$output = array(
'access_token' => $responseJSON['access_token'],
'expires_in' => $responseJSON['expires_in']
);
return response($output)
->header('Content-Type', 'application/json')
->cookie('refreshToken', $responseJSON['refresh_token'], 14400, null, null, true, true);
}
Access Token is set to 30 min and refresh token to 10 days. Everything works but I don't know if this is a good practice because:
- I read that JS applications can't save the access token securely
- Refresh Token should not be saved in cookies
- Laravel provides something for JS Frontends
- (and // or) For consuming the API as a first party app, this procedure is not needed
So my question is: What is a good/best practice for my kind of use case, so the authentication is secure and working on both (ReactJs Web-App and React-Native Mobile App).