py-learn / src /app /authentication /authentication.component.ts
Oviya
Deploy Angular to HF Space
e4ab6d0
raw
history blame
1.41 kB
import { Component } from '@angular/core';
import { AuthenticationService } from './authentication.service'; // Import the AuthenticationService
import { Router } from '@angular/router';
@Component({
selector: 'app-authentication',
templateUrl: './authentication.component.html',
styleUrl: './authentication.component.css'
})
export class AuthenticationComponent {
username: string = '';
password: string = '';
errorMessage: string = '';
constructor(private authService: AuthenticationService, private router: Router) { }
// ✅ Login functionality
login(): void {
this.authService.login(this.username, this.password).subscribe(
(response: any) => {
// Directly access the tokens from the response body
const token = response.access_token; // Access the access token directly from the body
this.authService.storeToken(token);
this.router.navigate(['/home']); // Redirect to the home page or dashboard after successful login
},
(error) => {
this.errorMessage = 'Invalid username or password'; // Show error message if login fails
}
);
}
// ✅ Check if the user is logged in (token is stored in localStorage)
get isLoggedIn(): boolean {
return this.authService.isLoggedIn();
}
showPassword: boolean = false;
togglePasswordVisibility(): void {
this.showPassword = !this.showPassword;
}
}