|
|
@@ -3,6 +3,7 @@ import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpErrorResponse
|
|
|
import { AuthService } from '@app/services/auth2.service';
|
|
|
import { Observable, throwError, BehaviorSubject } from 'rxjs';
|
|
|
import { catchError, filter, take, switchMap } from 'rxjs/operators';
|
|
|
+import Swal from 'sweetalert2';
|
|
|
|
|
|
@Injectable()
|
|
|
export class TokenInterceptor implements HttpInterceptor {
|
|
|
@@ -13,10 +14,10 @@ export class TokenInterceptor implements HttpInterceptor {
|
|
|
constructor(public authService: AuthService) { }
|
|
|
|
|
|
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
|
|
- if (request.url.indexOf("auth/refresh")!= -1){
|
|
|
+ if (request.url.indexOf("auth/refresh")!= -1 || request.url.indexOf("auth/logout2") != -1){
|
|
|
request = this.addToken(request, this.authService.getRefreshToken());
|
|
|
}
|
|
|
- else if (request.url.indexOf("openweather")!= -1){
|
|
|
+ else if (request.url.indexOf("openweather")!= -1 ){
|
|
|
request;
|
|
|
}
|
|
|
else {
|
|
|
@@ -26,11 +27,19 @@ export class TokenInterceptor implements HttpInterceptor {
|
|
|
}
|
|
|
|
|
|
return next.handle(request).pipe(catchError(error => {
|
|
|
-
|
|
|
// Erase storage if response code from auth refresh gives 401 or 422 error
|
|
|
if (request.url.indexOf("auth/refresh")!= -1 && error instanceof HttpErrorResponse){
|
|
|
+ Swal.fire({
|
|
|
+ allowOutsideClick: false,
|
|
|
+ type: 'warning',
|
|
|
+ title: 'La sesión ha expirado',
|
|
|
+ showConfirmButton: false,
|
|
|
+ });
|
|
|
this.authService.removeTokens();
|
|
|
- window.location.href="#/login";
|
|
|
+ setTimeout(function(){
|
|
|
+ Swal.close();
|
|
|
+ window.location.href = "#/login";
|
|
|
+ }, 2000);
|
|
|
}
|
|
|
|
|
|
if (error instanceof HttpErrorResponse && (error.status === 401 || error.status === 422)) {
|
|
|
@@ -53,7 +62,9 @@ export class TokenInterceptor implements HttpInterceptor {
|
|
|
|
|
|
// Cuando el endpoint devuelva 401 debe mandarlo al login y destruir las variables del storage
|
|
|
private handle4xxError(request: HttpRequest<any>, next: HttpHandler) {
|
|
|
+ console.log("handle error 400");
|
|
|
if (!this.isRefreshing) {
|
|
|
+ console.log("inside ")
|
|
|
this.isRefreshing = true;
|
|
|
this.refreshTokenSubject.next(null);
|
|
|
|
|
|
@@ -64,14 +75,12 @@ export class TokenInterceptor implements HttpInterceptor {
|
|
|
return next.handle(this.addToken(request, token["data"]["access_token"]));
|
|
|
}));
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
} else {
|
|
|
+ this.isRefreshing = false;
|
|
|
+
|
|
|
return this.refreshTokenSubject.pipe(
|
|
|
filter(token => token != null),
|
|
|
- take(1),
|
|
|
+ take(2),
|
|
|
switchMap(jwt => {
|
|
|
return next.handle(this.addToken(request, jwt));
|
|
|
}));
|