From dcea411f5b5e757d5523b3c91ced1f3977808c76 Mon Sep 17 00:00:00 2001 From: Cathy Hu Date: Mon, 17 Aug 2020 19:32:23 +0200 Subject: [PATCH] [frontend] Add access token interceptor --- kibicara-frontend/src/app/app.module.ts | 8 +++-- .../auth-token-interceptor.service.spec.ts | 16 ---------- .../auth/auth-token-interceptor.service.ts | 26 ----------------- .../core/auth/auth-token.interceptor.spec.ts | 18 ++++++++++++ .../app/core/auth/auth-token.interceptor.ts | 29 +++++++++++++++++++ .../src/app/core/auth/login.service.ts | 15 +++++----- .../src/app/dashboard/dashboard.module.ts | 8 ++--- 7 files changed, 63 insertions(+), 57 deletions(-) delete mode 100644 kibicara-frontend/src/app/core/auth/auth-token-interceptor.service.spec.ts delete mode 100644 kibicara-frontend/src/app/core/auth/auth-token-interceptor.service.ts create mode 100644 kibicara-frontend/src/app/core/auth/auth-token.interceptor.spec.ts create mode 100644 kibicara-frontend/src/app/core/auth/auth-token.interceptor.ts diff --git a/kibicara-frontend/src/app/app.module.ts b/kibicara-frontend/src/app/app.module.ts index 578a512..87679d4 100644 --- a/kibicara-frontend/src/app/app.module.ts +++ b/kibicara-frontend/src/app/app.module.ts @@ -10,12 +10,13 @@ import { DashboardModule } from './dashboard/dashboard.module'; import { HoodpageComponent } from './hoodpage/hoodpage.component'; import { HoodspageComponent } from './hoodspage/hoodspage.component'; import { ApiModule } from './core/api/api.module'; -import { HttpClientModule } from '@angular/common/http'; +import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http'; import { BASE_PATH } from './core/api/variables'; import { environment } from 'src/environments/environment'; import { LoginComponent } from './login/login.component'; import { RegisterComponent } from './register/register.component'; import { ReactiveFormsModule } from '@angular/forms'; +import { AuthTokenInterceptor } from './core/auth/auth-token.interceptor'; @NgModule({ declarations: [ @@ -36,7 +37,10 @@ import { ReactiveFormsModule } from '@angular/forms'; HttpClientModule, ReactiveFormsModule, ], - providers: [{ provide: BASE_PATH, useValue: environment.API_BASE_PATH }], + providers: [ + { provide: BASE_PATH, useValue: environment.API_BASE_PATH }, + { provide: HTTP_INTERCEPTORS, useClass: AuthTokenInterceptor, multi: true }, + ], bootstrap: [AppComponent], }) export class AppModule {} diff --git a/kibicara-frontend/src/app/core/auth/auth-token-interceptor.service.spec.ts b/kibicara-frontend/src/app/core/auth/auth-token-interceptor.service.spec.ts deleted file mode 100644 index c8274cc..0000000 --- a/kibicara-frontend/src/app/core/auth/auth-token-interceptor.service.spec.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { TestBed } from '@angular/core/testing'; - -import { AuthTokenInterceptorService } from './auth-token-interceptor.service'; - -describe('AuthTokenInterceptorService', () => { - let service: AuthTokenInterceptorService; - - beforeEach(() => { - TestBed.configureTestingModule({}); - service = TestBed.inject(AuthTokenInterceptorService); - }); - - it('should be created', () => { - expect(service).toBeTruthy(); - }); -}); diff --git a/kibicara-frontend/src/app/core/auth/auth-token-interceptor.service.ts b/kibicara-frontend/src/app/core/auth/auth-token-interceptor.service.ts deleted file mode 100644 index 1bb13c3..0000000 --- a/kibicara-frontend/src/app/core/auth/auth-token-interceptor.service.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { Injectable } from '@angular/core'; -import { HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http'; -import { Observable } from 'rxjs'; - -@Injectable({ - providedIn: 'root', -}) -export class AuthTokenInterceptorService { - constructor() {} - - intercept( - req: HttpRequest, - next: HttpHandler - ): Observable> { - // const token = this.loginService.token; - - // if (token) { - // req = req.clone({ - // setHeaders: { - // Authorization: 'Bearer ' + this.loginService.token, - // }, - // }); - // } - return next.handle(req); - } -} diff --git a/kibicara-frontend/src/app/core/auth/auth-token.interceptor.spec.ts b/kibicara-frontend/src/app/core/auth/auth-token.interceptor.spec.ts new file mode 100644 index 0000000..69c2e39 --- /dev/null +++ b/kibicara-frontend/src/app/core/auth/auth-token.interceptor.spec.ts @@ -0,0 +1,18 @@ +import { TestBed } from '@angular/core/testing'; + +import { AuthTokenInterceptor } from './auth-token.interceptor'; + +describe('AuthTokenInterceptor', () => { + beforeEach(() => + TestBed.configureTestingModule({ + providers: [AuthTokenInterceptor], + }) + ); + + it('should be created', () => { + const interceptor: AuthTokenInterceptor = TestBed.inject( + AuthTokenInterceptor + ); + expect(interceptor).toBeTruthy(); + }); +}); diff --git a/kibicara-frontend/src/app/core/auth/auth-token.interceptor.ts b/kibicara-frontend/src/app/core/auth/auth-token.interceptor.ts new file mode 100644 index 0000000..fe45675 --- /dev/null +++ b/kibicara-frontend/src/app/core/auth/auth-token.interceptor.ts @@ -0,0 +1,29 @@ +import { Injectable } from '@angular/core'; +import { + HttpRequest, + HttpHandler, + HttpEvent, + HttpInterceptor, +} from '@angular/common/http'; +import { Observable } from 'rxjs'; +import { LoginService } from './login.service'; + +@Injectable() +export class AuthTokenInterceptor implements HttpInterceptor { + constructor(private loginService: LoginService) {} + + intercept( + request: HttpRequest, + next: HttpHandler + ): Observable> { + const token = this.loginService.currentHoodAdminValue; + if (token) { + request = request.clone({ + setHeaders: { + Authorization: 'Bearer ' + token, + }, + }); + } + return next.handle(request); + } +} diff --git a/kibicara-frontend/src/app/core/auth/login.service.ts b/kibicara-frontend/src/app/core/auth/login.service.ts index ca0ace8..9c62b5b 100644 --- a/kibicara-frontend/src/app/core/auth/login.service.ts +++ b/kibicara-frontend/src/app/core/auth/login.service.ts @@ -1,18 +1,19 @@ import { Injectable } from '@angular/core'; import { BehaviorSubject, Observable } from 'rxjs'; import { map } from 'rxjs/operators'; -import { AdminService } from '../api'; +import { AdminService, BodyAccessToken } from '../api'; @Injectable({ providedIn: 'root', }) export class LoginService { - private currentHoodAdminSubject: BehaviorSubject; - public currentHoodAdmin: Observable; + private currentHoodAdminSubject: BehaviorSubject; + public currentHoodAdmin: Observable; + private identifier = 'currentHoodAdmin'; constructor(private readonly adminService: AdminService) { - this.currentHoodAdminSubject = new BehaviorSubject( - JSON.parse(localStorage.getItem('currentHoodAdmin')) + this.currentHoodAdminSubject = new BehaviorSubject( + JSON.parse(localStorage.getItem(this.identifier)) ); this.currentHoodAdmin = this.currentHoodAdminSubject.asObservable(); } @@ -24,7 +25,7 @@ export class LoginService { login(email, password) { return this.adminService.login(email, password).pipe( map((response) => { - localStorage.setItem('currentHoodAdmin', JSON.stringify(response)); + localStorage.setItem(this.identifier, JSON.stringify(response)); this.currentHoodAdminSubject.next(response); return response; }) @@ -32,7 +33,7 @@ export class LoginService { } logout() { - localStorage.removeItem('currentHoodAdmin'); + localStorage.removeItem(this.identifier); this.currentHoodAdminSubject.next(null); } } diff --git a/kibicara-frontend/src/app/dashboard/dashboard.module.ts b/kibicara-frontend/src/app/dashboard/dashboard.module.ts index 6733209..bb8ccad 100644 --- a/kibicara-frontend/src/app/dashboard/dashboard.module.ts +++ b/kibicara-frontend/src/app/dashboard/dashboard.module.ts @@ -4,12 +4,8 @@ import { HoodsComponent } from './hoods/hoods.component'; import { SettingspageComponent } from './settingspage/settingspage.component'; import { DashboardComponent } from './dashboard.component'; - - @NgModule({ declarations: [HoodsComponent, SettingspageComponent, DashboardComponent], - imports: [ - CommonModule - ] + imports: [CommonModule], }) -export class DashboardModule { } +export class DashboardModule {}