|
|
@ -1,92 +1,98 @@
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpErrorResponse } from '@angular/common/http';
|
|
|
|
import {
|
|
|
|
|
|
|
|
HttpRequest,
|
|
|
|
|
|
|
|
HttpHandler,
|
|
|
|
|
|
|
|
HttpEvent,
|
|
|
|
|
|
|
|
HttpInterceptor,
|
|
|
|
|
|
|
|
HttpErrorResponse,
|
|
|
|
|
|
|
|
} from '@angular/common/http';
|
|
|
|
import { AuthService } from '../services/auth.service';
|
|
|
|
import { AuthService } from '../services/auth.service';
|
|
|
|
import { Observable, throwError, BehaviorSubject } from 'rxjs';
|
|
|
|
import { Observable, throwError, BehaviorSubject } from 'rxjs';
|
|
|
|
import { catchError, filter, take, switchMap } from 'rxjs/operators';
|
|
|
|
import { catchError, filter, take, switchMap } from 'rxjs/operators';
|
|
|
|
import { SnackBarService } from '../services/snackbar.service';
|
|
|
|
import { SnackBarService } from '../services/snackbar.service';
|
|
|
|
import { Router, RouterStateSnapshot } from '@angular/router';
|
|
|
|
import { Router, RouterStateSnapshot } from '@angular/router';
|
|
|
|
import { JsonPipe } from '@angular/common';
|
|
|
|
import { getErrorMessage } from './getErrorMessage';
|
|
|
|
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root',
|
|
|
|
providedIn: 'root',
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
export class TokenInterceptor implements HttpInterceptor {
|
|
|
|
export class TokenInterceptor implements HttpInterceptor {
|
|
|
|
|
|
|
|
|
|
|
|
private isRefreshing = false;
|
|
|
|
private isRefreshing = false;
|
|
|
|
private requestTokenSubject: BehaviorSubject<any> = new BehaviorSubject<any>(null);
|
|
|
|
private requestTokenSubject: BehaviorSubject<any> = new BehaviorSubject<any>(
|
|
|
|
|
|
|
|
null
|
|
|
|
constructor(private authService: AuthService, private snackBar : SnackBarService, private router: Router) { }
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
|
|
|
constructor(
|
|
|
|
|
|
|
|
private authService: AuthService,
|
|
|
|
|
|
|
|
private snackBar: SnackBarService,
|
|
|
|
|
|
|
|
private router: Router
|
|
|
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
intercept(
|
|
|
|
|
|
|
|
request: HttpRequest<any>,
|
|
|
|
|
|
|
|
next: HttpHandler
|
|
|
|
|
|
|
|
): Observable<HttpEvent<any>> {
|
|
|
|
//console.log("i intercepted something");
|
|
|
|
//console.log("i intercepted something");
|
|
|
|
if (this.authService.getRequestToken()) {
|
|
|
|
if (this.authService.getRequestToken()) {
|
|
|
|
request = this.addToken(request, this.authService.getRequestToken());
|
|
|
|
request = this.addToken(request, this.authService.getRequestToken());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return next.handle(request).pipe(catchError((error: HttpErrorResponse) => {
|
|
|
|
return next.handle(request).pipe(
|
|
|
|
let errorMessage = '';
|
|
|
|
catchError((error: HttpErrorResponse) => {
|
|
|
|
if (error.error instanceof ErrorEvent) {
|
|
|
|
let errorMessage = '';
|
|
|
|
//client error
|
|
|
|
//server error
|
|
|
|
//console.log("Client Error: " + JSON.stringify(error));
|
|
|
|
//console.log("Server Error: " + JSON.stringify(error));
|
|
|
|
|
|
|
|
console.log(error);
|
|
|
|
errorMessage = `Error: ${error.error.message}`;
|
|
|
|
if (error?.status === 400) {
|
|
|
|
} else {
|
|
|
|
switch (error?.error?.message) {
|
|
|
|
//server error
|
|
|
|
case 'Invalid refresh token!':
|
|
|
|
//console.log("Server Error: " + JSON.stringify(error));
|
|
|
|
this.authService.logout();
|
|
|
|
if (error.status === 400){
|
|
|
|
errorMessage =
|
|
|
|
switch (error.error.message) {
|
|
|
|
'Die aktuelle Sitzung ist abgelaufen. Bitte loggen sie sich erneut ein.';
|
|
|
|
case "Invalid refresh token!":
|
|
|
|
this.router.navigate(['/login'], {
|
|
|
|
this.authService.logout();
|
|
|
|
queryParams: {
|
|
|
|
errorMessage = "Die aktuelle Sitzung ist abgelaufen. Bitte loggen sie sich erneut ein."
|
|
|
|
returnUrl: this.router.routerState.snapshot.url,
|
|
|
|
this.router.navigate(["/login"], { queryParams: { returnUrl: this.router.routerState.snapshot.url } });
|
|
|
|
},
|
|
|
|
break;
|
|
|
|
});
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
|
|
|
|
errorMessage = this.serverErrorMessageGenerator(error);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
errorMessage = getErrorMessage(error);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
} else if (error.status === 401) {
|
|
|
|
} else if (error?.status === 401) {
|
|
|
|
var urlSplit : string[] = error.url.split("/");
|
|
|
|
var urlSplit: string[] = error.url.split('/'); //TODO: UFF might lead to bug when deployed under sub path .../frontend/...
|
|
|
|
if (urlSplit[3] === "users" && urlSplit[5] === "update"){ // Allow user pw updates to be processed correctly
|
|
|
|
if (urlSplit[3] === 'users' && urlSplit[5] === 'update') {
|
|
|
|
errorMessage = "Das aktuelle Passwort ist inkorrekt.";
|
|
|
|
// Allow user pw updates to be processed correctly
|
|
|
|
} else {
|
|
|
|
errorMessage = 'Das aktuelle Passwort ist inkorrekt.';
|
|
|
|
return this.handle401Error(request, next);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
errorMessage = this.serverErrorMessageGenerator(error);
|
|
|
|
return this.handle401Error(request, next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
errorMessage = getErrorMessage(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (errorMessage === "Viele Fehler sind aufgetreten.") { // Here is the thing I ment
|
|
|
|
|
|
|
|
this.snackBar.openSnackBar(errorMessage, "Erweitert", true, error.error.errors);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
this.snackBar.openSnackBar(errorMessage, "Ok", true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return throwError(errorMessage);
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private serverErrorMessageGenerator (error: HttpErrorResponse): string {
|
|
|
|
if (error?.error?.errors?.length > 1) {
|
|
|
|
if (error.error.message === undefined){
|
|
|
|
this.snackBar.openSnackBar(
|
|
|
|
if (error.error.errors[0].message.includes("not provided")) {
|
|
|
|
errorMessage,
|
|
|
|
return "Nicht alle benötigten Felder wurden ausgefüllt.";
|
|
|
|
'Erweitert',
|
|
|
|
} else {
|
|
|
|
true,
|
|
|
|
return "Viele Fehler sind aufgetreten." // If you change this you have to change it over this aswell ( I know its terrible to @ )
|
|
|
|
error.error.errors
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return `${error.error.message}.`; //`${error.error.message}. Fehlercode: ${error.status}.`
|
|
|
|
this.snackBar.openSnackBar(errorMessage, 'Ok', true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return throwError(errorMessage);
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private addToken(request: HttpRequest<any>, token: string) {
|
|
|
|
private addToken(request: HttpRequest<any>, token: string) {
|
|
|
|
return request.clone({
|
|
|
|
return request.clone({
|
|
|
|
setHeaders: {
|
|
|
|
setHeaders: {
|
|
|
|
'Authorization': `Bearer ${token}`
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -100,15 +106,16 @@ export class TokenInterceptor implements HttpInterceptor {
|
|
|
|
this.isRefreshing = false;
|
|
|
|
this.isRefreshing = false;
|
|
|
|
this.requestTokenSubject.next(token.request_token);
|
|
|
|
this.requestTokenSubject.next(token.request_token);
|
|
|
|
return next.handle(this.addToken(request, token.request_token));
|
|
|
|
return next.handle(this.addToken(request, token.request_token));
|
|
|
|
}));
|
|
|
|
})
|
|
|
|
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return this.requestTokenSubject.pipe(
|
|
|
|
return this.requestTokenSubject.pipe(
|
|
|
|
filter(token => token != null),
|
|
|
|
filter((token) => token != null),
|
|
|
|
take(1),
|
|
|
|
take(1),
|
|
|
|
switchMap(jwt => {
|
|
|
|
switchMap((jwt) => {
|
|
|
|
return next.handle(this.addToken(request, jwt));
|
|
|
|
return next.handle(this.addToken(request, jwt));
|
|
|
|
}));
|
|
|
|
})
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|