Skip to content

Commit

Permalink
Circular dependency in HTTP_INTERCEPTORS
Browse files Browse the repository at this point in the history
  • Loading branch information
litvinets committed Mar 3, 2021
1 parent ac5445a commit 01d529f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
35 changes: 16 additions & 19 deletions src/app/shared/interceptors/http-token.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { catchError } from 'rxjs/operators';
import { OidcSecurityService } from 'angular-auth-oidc-client';
import { Select } from '@ngxs/store';
import { AuthService } from '../services/org-cards/auth.service';
import { Observable, throwError } from 'rxjs';
import { Select, Store } from '@ngxs/store';
import { UserRegistrationState } from '../store/user-registration.state';

@Injectable()
export class HttpTokenInterceptor implements HttpInterceptor {
constructor(
private oidcSecurityService: OidcSecurityService,
private http: HttpClient) {}
constructor(
public auth: AuthService,
public store: Store) {}

@Select(UserRegistrationState.role)
role$: Observable<string>;
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
let token = this.oidcSecurityService.getToken();
const role = this.oidcSecurityService.getToken()['role'];
const tokenTitle = (role === this.role$) ? 'Authorization' : 'token';
@Select(UserRegistrationState.role)
role$: Observable<string>;

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

if (this.role$ === role && token !== null) {
let token = this.auth.getToken();
const role = this.store.selectSnapshot<string>(UserRegistrationState.role);
const tokenTitle = (role) ? 'token': 'Authorization';
alert('Http token interceptor works!')
if (role && token !== null) {
token = `Bearer ${token}`;
console.log('This thing works')
}
if (typeof token === 'string') {
return next.handle(request.clone({
Expand Down
8 changes: 8 additions & 0 deletions src/app/shared/services/org-cards/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Injectable } from '@angular/core';

@Injectable()
export class AuthService {
public getToken(): string {
return localStorage.getItem('token');
}
}

0 comments on commit 01d529f

Please sign in to comment.