import { Component, OnInit } from '@angular/core'; import { FormBuilder } from '@angular/forms'; import { Router } from '@angular/router'; import { Subscription, Observable } from 'rxjs'; import { AuthenticationService, AuthLevel } from '../../../authentication.service'; import { RoleService, Roles } from '../../../role.service'; @Component({ selector: 'app-login', templateUrl: './login.component.html', styleUrls: ['./login.component.css'], }) export class LoginComponent implements OnInit { public auth: AuthLevel = AuthLevel.None; private roles = Roles; public error: boolean = false; public errorMsg: string = ""; loginForm = this.formBuilder.group({ email: '', pw: '' }); constructor(private authenticationService: AuthenticationService, private roleService: RoleService, private formBuilder: FormBuilder, private route: Router) {} ngOnInit(): void { this.authenticationService.status.subscribe( (result: AuthLevel) => { this.auth = result; }); } async authenticate() { if(this.loginForm.value.email == "" || this.loginForm.value.pw == "") { this.error = true; this.errorMsg = "Email or Password left empty"; } else { this.error = false; this.errorMsg = ""; let result: any = await this.authenticationService.authenticate(this.loginForm.value.email, this.loginForm.value.pw); console.log(result); if(result.result === "Success") { this.roleService.changeRole(parseInt(result.data.role)); this.authenticationService.swapAuth(result.data.auth); this.loginForm.reset(); if(result.data.role == 1) { this.route.navigateByUrl('/manager-home'); } else { this.route.navigateByUrl(''); } } else { this.error = true; this.errorMsg = "Invalid email or password"; } } } }