After a long time I didn't find an approach about refresing tokens TTL: 30 minutes Refresh TTL: 2 Weeks
If I refresh the page after 45 minutes innactive then I make a getAccessToken() function to send the expired token and then send me back a refreshed token. The big problem is that if my page makes more than 1 ajax request then if the first request invalidate my token the second request force my to relogin becauce it send empty token
@NgModule({
providers: [
{
provide: AuthHttp,
useFactory: authHttpServiceFactory,
deps: [Http, RequestOptions, Router]
}
]
})
export function authHttpServiceFactory(http: Http, options: RequestOptions, router: Router) {
return new AuthHttp(new AuthConfig({
tokenName: 'token',
tokenGetter: (() => getAccessToken(http,router)),
//tokenGetter: (() => localStorage.getItem('JWToken')),
globalHeaders: [{'Content-Type': 'application/json'}],
noJwtError: true,
}), http, options);
}
function getAccessToken(http: Http, router:Router): Promise<string> {
let jwtHelper: JwtHelper = new JwtHelper();
let accessToken = localStorage.getItem('JWToken');
if( accessToken == '' || !accessToken || accessToken == undefined || accessToken == null){
router.navigate(['./admin/login']);
return;
}
if (jwtHelper.isTokenExpired(accessToken)) {
return new Promise((resolve, reject) => {
let refreshTokenService: RefreshTokenService = new RefreshTokenService(http);
refreshTokenService.refreshToken(accessToken).subscribe((res: any) => {
res = res.json();
if(res.token) {
localStorage.setItem('JWToken', res.token);
resolve(res.token);
}else{
localStorage.removeItem('JWToken');
router.navigate(['./admin/login']);
}
});
});
} else {
return Promise.resolve(accessToken);
}
}
I want the requests wait for the response of the first request
And this is the injector for the new httpClient library
And do not forget to send cache (browser) headers to the backend response for at least some seconds.