File size: 1,460 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
49
50
51
52
53
54
55
56
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;
  }


}