Skip to content

Commit

Permalink
auth service. This version doesnt work
Browse files Browse the repository at this point in the history
  • Loading branch information
litvinets committed Mar 2, 2021
1 parent 958cdba commit 5cd2692
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 28 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@ngxs/store": "^3.7.1",
"@types/leaflet": "1",
"angular-auth-oidc-client": "^11.5.0",
"angular2-jwt": "^0.2.3",
"jwt-decode": "^3.1.2",
"leaflet": "^1.7.1",
"leaflet-control-geocoder": "^2.1.0",
Expand Down
59 changes: 31 additions & 28 deletions src/app/shared/interceptors/http-token.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,43 @@ import { Injectable } from '@angular/core';
import { HttpClient, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
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 { 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) {}
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>;

if (this.role$ === role && token !== null) {
token = `Bearer ${token}`;
console.log('This thing works')
}
if (typeof token === 'string') {
return next.handle(request.clone({
setHeaders: { [tokenTitle]: token }
})).pipe(catchError((error) => {
return throwError(error);
}));
}
return next.handle(request).pipe(catchError((error) => {
return throwError(error);
}));
}
}
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
let token = this.auth.getToken();
const role = this.store.selectSnapshot<string>(UserRegistrationState.role);
const tokenTitle = (role) ? 'token': 'Authorization';
// 'token !== null' added to avoid pass condition when "typeof token === 'string'" and token is set to 'Bearer null'
if (role && token !== null) {
token = `Bearer ${token}`;
console.log('Http token interceptor works!')
}
if (typeof token === 'string') {
return next.handle(request.clone({
setHeaders: { [tokenTitle]: token }
})).pipe(catchError((error) => {
return throwError(error);
}));
}
return next.handle(request).pipe(catchError((error) => {
return throwError(error);
}));
}
}
17 changes: 17 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,17 @@
import { Injectable } from '@angular/core';
import { tokenNotExpired } from 'angular2-jwt';

@Injectable({
providedIn: 'root'
})
export class AuthService {
constructor() { }

getToken(): string {
return localStorage.getItem('token');
}
isAuthenticated(): boolean {
const token = this.getToken();
return tokenNotExpired(null, token);
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2014,6 +2014,11 @@ angular-auth-oidc-client@^11.5.0:
jsrsasign-reduced "^8.0.15"
tslib "^2.0.0"

angular2-jwt@^0.2.3:
version "0.2.3"
resolved "https://registry.yarnpkg.com/angular2-jwt/-/angular2-jwt-0.2.3.tgz#54efdda3ceedba85f6a37b165f22ac22b8adf021"
integrity sha1-VO/do87tuoX2o3sWXyKsIrit8CE=

ansi-colors@4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/ansi-colors/-/ansi-colors-4.1.1.tgz#cbb9ae256bf750af1eab344f229aa27fe94ba348"
Expand Down

0 comments on commit 5cd2692

Please sign in to comment.