Début d'ajout pour la gestion du login
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
import { AuthenticationService } from '../services/authentication.service';
|
||||
|
||||
@Injectable()
|
||||
export class BasicAuthInterceptor implements HttpInterceptor {
|
||||
constructor(private authenticationService: AuthenticationService) { }
|
||||
|
||||
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||
// add authorization header with basic auth credentials if available
|
||||
const currentUser = this.authenticationService.currentUserValue;
|
||||
if (currentUser && currentUser.authdata) {
|
||||
request = request.clone({
|
||||
setHeaders: {
|
||||
Authorization: `Basic ${currentUser.authdata}`
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return next.handle(request);
|
||||
}
|
||||
}
|
||||
39
Front/skydivelogs-app/src/interceptor/caching.interceptor.ts
Normal file
39
Front/skydivelogs-app/src/interceptor/caching.interceptor.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import {
|
||||
HttpEvent,
|
||||
HttpRequest,
|
||||
HttpResponse,
|
||||
HttpInterceptor,
|
||||
HttpHandler
|
||||
} from '@angular/common/http';
|
||||
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import { startWith, tap } from 'rxjs/operators';
|
||||
import 'rxjs/add/observable/of';
|
||||
|
||||
import { RequestCache } from '../services/request-cache.service';
|
||||
|
||||
@Injectable()
|
||||
export class CachingInterceptor implements HttpInterceptor {
|
||||
constructor(private cache: RequestCache) { }
|
||||
|
||||
intercept(req: HttpRequest<any>, next: HttpHandler) {
|
||||
const cachedResponse = this.cache.get(req);
|
||||
return cachedResponse
|
||||
? Observable.of(cachedResponse)
|
||||
: this.sendRequest(req, next);
|
||||
}
|
||||
|
||||
sendRequest(
|
||||
req: HttpRequest<any>,
|
||||
next: HttpHandler
|
||||
): Observable<HttpEvent<any>> {
|
||||
return next.handle(req).pipe(
|
||||
tap(event => {
|
||||
if (event instanceof HttpResponse) {
|
||||
this.cache.put(req, event);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
24
Front/skydivelogs-app/src/interceptor/error.interceptor.ts
Normal file
24
Front/skydivelogs-app/src/interceptor/error.interceptor.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
|
||||
import { Observable, throwError } from 'rxjs';
|
||||
import { catchError } from 'rxjs/operators';
|
||||
|
||||
import { AuthenticationService } from '../services/authentication.service';
|
||||
|
||||
@Injectable()
|
||||
export class ErrorInterceptor implements HttpInterceptor {
|
||||
constructor(private authenticationService: AuthenticationService) { }
|
||||
|
||||
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||
return next.handle(request).pipe(catchError(err => {
|
||||
if (err.status === 401) {
|
||||
// auto logout if 401 response returned from api
|
||||
this.authenticationService.logout();
|
||||
location.reload(true);
|
||||
}
|
||||
|
||||
const error = err.error.message || err.statusText;
|
||||
return throwError(error);
|
||||
}))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user