File size: 1,413 Bytes
e4ab6d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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;
  }



}