| import { Component } from '@angular/core'; | |
| import { AuthService } from './auth.service'; | |
| import { Router } from '@angular/router'; | |
| ({ | |
| 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) { } | |
| //ngOnInit(): void { | |
| // this.authService.checkSession(); | |
| //} | |
| // β Handle Login | |
| login(): void { | |
| this.authService.login(this.username, this.password).subscribe( | |
| (response) => { | |
| console.log('β Login success. Redirecting to /home...'); | |
| this.authService.setLoggedIn(true); | |
| this.authService.startAutoRefresh(); | |
| /*this.router.navigate(['/home']); // Redirect to dashboard after login*/ | |
| // π Redirect to stored URL or default to /home | |
| const redirectUrl = localStorage.getItem('redirectAfterLogin') || '/home'; | |
| localStorage.removeItem('redirectAfterLogin'); | |
| this.router.navigate([redirectUrl]); | |
| }, | |
| (error) => { | |
| this.errorMessage = 'Invalid username or password'; | |
| } | |
| ); | |
| } | |
| // β Check if user is logged in | |
| isLoggedIn(): boolean { | |
| return this.authService.isLoggedIn(); | |
| } | |
| togglePasswordVisibility(): void { | |
| this.showPassword = !this.showPassword; | |
| } | |
| } | |