php - How to keep authenticated with an API using JWT -
i writing application in laravel5 communicates api, how can keep authenticated api after jwt token expired. login credentials stored in environment file. first time login , save token session. have token can expire , returns 401 message
{ "message": "token expired" }
how can detect without user noticing application had re-authenticate, can use middleware catch 401 message token expired , authenticate again , store new token?
the flow this.
user requests url on application -> application asks data api -> checks token in storage 1 in api -> returns 401 message token expired -> requests new token -> returns data api user.
i have , user should not notice , don't want authenticate api on every request. have no idea start.
good question.
this can achieved via refresh tokens, if you're using laravel jwt package found:
you able utilise refresh tokens realtively easily. can add middleware like:
protected $routemiddleware = [ 'jwt.refresh' => 'tymon\jwtauth\middleware\refreshtoken', ];
during step:
checks token in storage 1 in api
if token has expired refresh time still valid, returned refresh token in header, assuming route hit in jwt.refresh middleware.
let me know if need additional help.
full docs here:
Comments
Post a Comment