[frontend] Add confirmation component logic

This commit is contained in:
Cathy Hu 2020-08-18 23:47:08 +02:00
parent 8beabb9031
commit 165263e559
5 changed files with 71 additions and 2 deletions

View file

@ -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],
})

View file

@ -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<ConfirmComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ConfirmComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ConfirmComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View file

@ -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']);
}
}
}