0
votes

I have Web Api and AngularJS to consume web Api.

when user login with credential user get access_token and refresh token with issued and expires field.

access_token is expired in each 1 minutes and allocate new token to user.

now the problem is

The time between token expired and allocation of new token to user.

if user do a page refresh then its makes an api call to load data of that page, but the access token was just expired and user does not got a new token, so old token is set in header of api call, hence user got 401 unauthorized as response and application throw user to log out.

I am using token in first time so not have much information about access_token and refresh_token

So, I do not know how to handle this situation.

advises is appreciable.

2
Its simple just make the token request api call on page load and after that call is complete then only initilaise the angular app - Kushal
that means each time user reload page its make an api call for token, but I only want to assign new token when user reload page and got 401 status code. - gaurav bhavsar

2 Answers

0
votes

Whenever you make an API call to load data on the page, in the callback you should check the status code. If the status code is 401, get the refresh token and then make another call to the same API and then only initialize the app. Otherwise initialize the app with the old response value.

0
votes

Thanks @Kushal and @sahil to provide idea.

When user's token is expired app redirect them to login page so on login page added api call to fetch token by user's refresh_token and if user has correct refresh token then assign them to new token and redirect to page which user refreshed by tracking / maintain log of current page of user are. and its working.

Thanks again.