|
|
import { Component } from '@angular/core'; |
|
|
import { AuthService } from './auth.service'; |
|
|
import { Router } from '@angular/router'; |
|
|
type GradeLevel = 'lowergrade' | 'midgrade' | 'highergrade'; |
|
|
@Component({ |
|
|
selector: 'app-auth', |
|
|
templateUrl: './auth.component.html', |
|
|
styleUrl: './auth.component.css' |
|
|
}) |
|
|
export class AuthComponent { |
|
|
|
|
|
username: string = ''; |
|
|
password: string = ''; |
|
|
errorMessage: string = ''; |
|
|
showPassword: boolean = false; |
|
|
|
|
|
constructor(private authService: AuthService, private router: Router) { } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
login(): void { |
|
|
this.authService.login(this.username, this.password).subscribe( |
|
|
(response) => { |
|
|
|
|
|
let level = this.normalizeGrade( |
|
|
(response?.gradeLevel ?? response?.grade ?? response?.role ?? '') |
|
|
); |
|
|
|
|
|
|
|
|
if (!level) level = this.normalizeGrade(this.username); |
|
|
|
|
|
|
|
|
if (!level) level = 'highergrade'; |
|
|
localStorage.setItem('gradeLevel', level); |
|
|
|
|
|
|
|
|
this.authService.setLoggedIn(true); |
|
|
this.authService.startAutoRefresh(); |
|
|
|
|
|
const redirectUrl = localStorage.getItem('redirectAfterLogin') || '/home'; |
|
|
localStorage.removeItem('redirectAfterLogin'); |
|
|
this.router.navigate([redirectUrl]); |
|
|
}, |
|
|
() => { |
|
|
this.errorMessage = 'Invalid username or password'; |
|
|
} |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
private normalizeGrade(v: string): GradeLevel | '' { |
|
|
const s = (v || '').trim().toLowerCase(); |
|
|
if (s === 'lowergrade' || s === 'lower' || s === 'low' || s === 'l') return 'lowergrade'; |
|
|
if (s === 'midgrade' || s === 'mid' || s === 'm') return 'midgrade'; |
|
|
if (s === 'highergrade' || s === 'higher' || s === 'high' || s === 'h') return 'highergrade'; |
|
|
return ''; |
|
|
} |
|
|
|
|
|
|
|
|
isLoggedIn(): boolean { |
|
|
return this.authService.isLoggedIn(); |
|
|
} |
|
|
|
|
|
|
|
|
togglePasswordVisibility(): void { |
|
|
this.showPassword = !this.showPassword; |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|