py-learn / src /app /auth /auth.component.ts
Oviya
Deploy Angular to HF Space
e4ab6d0
raw
history blame
1.46 kB
import { Component } from '@angular/core';
import { AuthService } from './auth.service';
import { Router } from '@angular/router';
@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) { }
//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;
}
}