refresh token for api requests (401)

This commit is contained in:
Efstratios Giannopoulos 2023-10-12 18:29:27 +03:00
parent d14107062a
commit 9c8488febb
1 changed files with 32 additions and 4 deletions

View File

@ -1,8 +1,8 @@
import { HttpErrorResponse, HttpHandler, HttpHeaderResponse, HttpProgressEvent, HttpRequest, HttpResponse, HttpSentEvent, HttpUserEvent } from '@angular/common/http'; import { HttpErrorResponse, HttpHandler, HttpHeaderResponse, HttpProgressEvent, HttpRequest, HttpResponse, HttpSentEvent, HttpUserEvent } from '@angular/common/http';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { Observable, throwError } from 'rxjs'; import { Observable, from, throwError } from 'rxjs';
import { catchError, mergeMap, tap } from 'rxjs/operators'; import { catchError, filter, mergeMap, tap } from 'rxjs/operators';
import { AuthService } from '../../../app/core/services/auth/auth.service'; import { AuthService } from '../../../app/core/services/auth/auth.service';
import { BaseInterceptor } from './base.interceptor'; import { BaseInterceptor } from './base.interceptor';
import { InterceptorType } from './interceptor-type'; import { InterceptorType } from './interceptor-type';
@ -19,14 +19,15 @@ export class UnauthorizedResponseInterceptor extends BaseInterceptor {
get type(): InterceptorType { return InterceptorType.UnauthorizedResponse; } get type(): InterceptorType { return InterceptorType.UnauthorizedResponse; }
private accountRefresh$: Observable<boolean> = null;
interceptRequest(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> { interceptRequest(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
return next.handle(req).pipe( return next.handle(req).pipe(
catchError(error => { catchError(error => {
if (error instanceof HttpErrorResponse) { if (error instanceof HttpErrorResponse) {
switch ((<HttpErrorResponse>error).status) { switch ((<HttpErrorResponse>error).status) {
case 401: case 401:
this.logoutUser(); return this.handle401Error(req, next);
return throwError(error);
default: default:
return throwError(error); return throwError(error);
} }
@ -36,6 +37,33 @@ export class UnauthorizedResponseInterceptor extends BaseInterceptor {
})); }));
} }
private handle401Error(req: HttpRequest<any>, next: HttpHandler) {
if (!this.accountRefresh$) {
this.accountRefresh$ = from(
this.authService.refreshToken().then((isRefreshed) => {
this.accountRefresh$ = null;
if (!isRefreshed) {
this.logoutUser();
return false;
}
return true;
})
).pipe(filter((x) => x));
}
return this.accountRefresh$.pipe(mergeMap(account => this.repeatRequest(req, next)));
}
private repeatRequest(originalRequest: HttpRequest<any>, next: HttpHandler) {
const newAuthenticationToken: String = this.authService.currentAuthenticationToken();
const newRequest = originalRequest.clone({
setHeaders: {
Authorization: `Bearer ${newAuthenticationToken}`
}
});
return next.handle(newRequest);
}
private logoutUser() { private logoutUser() {
//this.authService.clear(); //this.authService.clear();
if (!this.isLoginRoute() && !this.isSignupRoute()) { this.router.navigate(['/unauthorized']); } if (!this.isLoginRoute() && !this.isSignupRoute()) { this.router.navigate(['/unauthorized']); }