I am using Ionic and satellizer along with Laravel and JWT auth to create an API.
Everything is good, but the one issue is the token being removed from local storage after an hour or so.
I really want the token to exist until the user logs out, as they will be using a phone app and not wishing to log in everytime.
This is first experience with tokens, so I am not sure on how this normally works. I imagine people done normally store tokens for ever?
This is in my Ionic controller:
$auth.login(credentials).then(function() {
$http.get($rootScope.apiURL + 'authenticate/user').success(function(response){
var user = JSON.stringify(response.user);
localStorage.setItem('user', user);
});
})
This sets a Satellizer token and also the user information in Local storage.
In Laravel for the API call:
public function authenticate(Request $request)
{
$credentials = $request->only('email', 'password');
try {
// verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json([
'error' => 'invalid_credentials',
'error_message' => 'Invalid username or password'
], 401);
}
} catch (JWTException $e) {
// something went wrong
return response()->json(['error' => 'could_not_create_token'], 500);
}
// if no errors are encountered we can return a JWT
return response()->json(compact('token'));
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire