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:

here

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:

docs


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -