import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { tap, switchMap, catchError } from 'rxjs/operators';
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return this.cognitoUserPool.getCurrentSession().pipe(
catchError(err => {
return of(false);
})
).pipe(
switchMap(res => {
if (res) {
req = req.clone({
withCredentials: true,
headers: req.headers.set('Authorization', 'Bearer ' + res.idToken.jwtToken)
});
}
return next.handle(req).pipe(
tap({
next: (event) => {
if (event instanceof HttpResponse) {
if (event.headers.has('Authorization')) {
const authHeaderString = event.headers.get('Authorization');
XXXX
}
}
},
error: (error) => {
if (error instanceof HttpErrorResponse) {
if (error.status === 401) {
XXXX
}
}
}
})
);
})
);
}
网友评论