From aaaa74c118897ae1af7f55a5bfc5f2454a59fccb Mon Sep 17 00:00:00 2001 From: Oleksandra Lytvynets Date: Wed, 3 Mar 2021 06:18:49 +0200 Subject: [PATCH] another one method --- .../interceptors/http-token.interceptor.ts | 48 ++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/src/app/shared/interceptors/http-token.interceptor.ts b/src/app/shared/interceptors/http-token.interceptor.ts index 56e7fff8d8..f4f0265315 100644 --- a/src/app/shared/interceptors/http-token.interceptor.ts +++ b/src/app/shared/interceptors/http-token.interceptor.ts @@ -1,6 +1,6 @@ import { Injectable } from '@angular/core'; -import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http'; -import { catchError } from 'rxjs/operators'; +import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpResponse, HttpErrorResponse } from '@angular/common/http'; +import { catchError, tap } from 'rxjs/operators'; import { AuthService } from '../services/org-cards/auth.service'; import { Observable, throwError } from 'rxjs'; import { Select, Store } from '@ngxs/store'; @@ -12,13 +12,14 @@ export class HttpTokenInterceptor implements HttpInterceptor { public auth: AuthService, public store: Store) {} - @Select(UserRegistrationState.role) - role$: Observable; + //@Select(UserRegistrationState.role) + //role$: Observable; intercept(request: HttpRequest, next: HttpHandler): Observable> { let token = this.auth.getToken(); - const role = this.store.selectSnapshot(UserRegistrationState.role); + //const role = this.store.selectSnapshot(UserRegistrationState.role); + const role ='provider'; const tokenTitle = (role) ? 'token': 'Authorization'; alert('Http token interceptor works!') if (role && token !== null) { @@ -35,4 +36,39 @@ export class HttpTokenInterceptor implements HttpInterceptor { return throwError(error); })); } -} \ No newline at end of file +} + +// @Injectable() +// export class HttpTokenInterceptor implements HttpInterceptor { +// constructor(public auth: AuthService) {} + +// intercept( +// req: HttpRequest, +// next: HttpHandler +// ): Observable> { + +// let token = this.auth.getToken(); +// //const role = this.store.selectSnapshot(UserRegistrationState.role); +// const role ='provider'; +// const tokenTitle = (role) ? 'token': 'Authorization'; + +// const authReq = req.clone({ +// headers: req.headers.set( tokenTitle, token ), +// }) + +// return next.handle(authReq).pipe( +// tap( +// (event) => { +// if (event instanceof HttpResponse) +// console.log('Server response') +// }, +// (err) => { +// if (err instanceof HttpErrorResponse) { +// if (err.status == 401) +// console.log('Unauthorized') +// } +// } +// ) +// ) +// } +// } \ No newline at end of file