Skip to content

Commit

Permalink
another one method
Browse files Browse the repository at this point in the history
  • Loading branch information
litvinets committed Mar 3, 2021
1 parent 01d529f commit aaaa74c
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions src/app/shared/interceptors/http-token.interceptor.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -12,13 +12,14 @@ export class HttpTokenInterceptor implements HttpInterceptor {
public auth: AuthService,
public store: Store) {}

@Select(UserRegistrationState.role)
role$: Observable<string>;
//@Select(UserRegistrationState.role)
//role$: Observable<string>;

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

let token = this.auth.getToken();
const role = this.store.selectSnapshot<string>(UserRegistrationState.role);
//const role = this.store.selectSnapshot<string>(UserRegistrationState.role);
const role ='provider';
const tokenTitle = (role) ? 'token': 'Authorization';
alert('Http token interceptor works!')
if (role && token !== null) {
Expand All @@ -35,4 +36,39 @@ export class HttpTokenInterceptor implements HttpInterceptor {
return throwError(error);
}));
}
}
}

// @Injectable()
// export class HttpTokenInterceptor implements HttpInterceptor {
// constructor(public auth: AuthService) {}

// intercept(
// req: HttpRequest<any>,
// next: HttpHandler
// ): Observable<HttpEvent<any>> {

// let token = this.auth.getToken();
// //const role = this.store.selectSnapshot<string>(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')
// }
// }
// )
// )
// }
// }

0 comments on commit aaaa74c

Please sign in to comment.