From 165263e55919f81701cbaa7a17682e34aacb5451 Mon Sep 17 00:00:00 2001 From: Cathy Hu Date: Tue, 18 Aug 2020 23:47:08 +0200 Subject: [PATCH] [frontend] Add confirmation component logic --- kibicara-frontend/src/app/app.module.ts | 8 +++- .../app/auth/confirm/confirm.component.html | 0 .../app/auth/confirm/confirm.component.scss | 0 .../auth/confirm/confirm.component.spec.ts | 25 ++++++++++++ .../src/app/auth/confirm/confirm.component.ts | 40 +++++++++++++++++++ 5 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 kibicara-frontend/src/app/auth/confirm/confirm.component.html create mode 100644 kibicara-frontend/src/app/auth/confirm/confirm.component.scss create mode 100644 kibicara-frontend/src/app/auth/confirm/confirm.component.spec.ts create mode 100644 kibicara-frontend/src/app/auth/confirm/confirm.component.ts diff --git a/kibicara-frontend/src/app/app.module.ts b/kibicara-frontend/src/app/app.module.ts index 87679d4..bf4ac6b 100644 --- a/kibicara-frontend/src/app/app.module.ts +++ b/kibicara-frontend/src/app/app.module.ts @@ -13,10 +13,12 @@ import { ApiModule } from './core/api/api.module'; 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 { LoginComponent } from './auth/login/login.component'; +import { RegisterComponent } from './auth/register/register.component'; import { ReactiveFormsModule } from '@angular/forms'; import { AuthTokenInterceptor } from './core/auth/auth-token.interceptor'; +import { ConfirmComponent } from './auth/confirm/confirm.component'; +import { ErrorInterceptor } from './core/auth/error.interceptor'; @NgModule({ declarations: [ @@ -27,6 +29,7 @@ import { AuthTokenInterceptor } from './core/auth/auth-token.interceptor'; HoodspageComponent, LoginComponent, RegisterComponent, + ConfirmComponent, ], imports: [ BrowserModule, @@ -40,6 +43,7 @@ import { AuthTokenInterceptor } from './core/auth/auth-token.interceptor'; providers: [ { provide: BASE_PATH, useValue: environment.API_BASE_PATH }, { provide: HTTP_INTERCEPTORS, useClass: AuthTokenInterceptor, multi: true }, + { provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true }, ], bootstrap: [AppComponent], }) diff --git a/kibicara-frontend/src/app/auth/confirm/confirm.component.html b/kibicara-frontend/src/app/auth/confirm/confirm.component.html new file mode 100644 index 0000000..e69de29 diff --git a/kibicara-frontend/src/app/auth/confirm/confirm.component.scss b/kibicara-frontend/src/app/auth/confirm/confirm.component.scss new file mode 100644 index 0000000..e69de29 diff --git a/kibicara-frontend/src/app/auth/confirm/confirm.component.spec.ts b/kibicara-frontend/src/app/auth/confirm/confirm.component.spec.ts new file mode 100644 index 0000000..4ddced9 --- /dev/null +++ b/kibicara-frontend/src/app/auth/confirm/confirm.component.spec.ts @@ -0,0 +1,25 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ConfirmComponent } from './confirm.component'; + +describe('ConfirmComponent', () => { + let component: ConfirmComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ ConfirmComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(ConfirmComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/kibicara-frontend/src/app/auth/confirm/confirm.component.ts b/kibicara-frontend/src/app/auth/confirm/confirm.component.ts new file mode 100644 index 0000000..73400c6 --- /dev/null +++ b/kibicara-frontend/src/app/auth/confirm/confirm.component.ts @@ -0,0 +1,40 @@ +import { Component, OnInit } from '@angular/core'; +import { ActivatedRoute, Router } from '@angular/router'; +import { AdminService } from '../../core/api'; +import { first } from 'rxjs/operators'; + +@Component({ + selector: 'app-confirm', + templateUrl: './confirm.component.html', + styleUrls: ['./confirm.component.scss'], +}) +export class ConfirmComponent implements OnInit { + constructor( + private route: ActivatedRoute, + private adminService: AdminService, + private router: Router + ) {} + + ngOnInit(): void { + const token = this.route.snapshot.queryParams['token']; + if (token) { + this.adminService + .confirm(token) + .pipe(first()) + .subscribe( + (data) => { + this.router.navigate(['/login'], { + queryParams: { registered: true }, + }); + }, + (error) => { + this.router.navigate(['/register'], { + queryParams: { error: true }, + }); + } + ); + } else { + this.router.navigate(['/register']); + } + } +}