import { Component, OnInit, inject } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormControl, FormsModule, ReactiveFormsModule, Validators, } from '@angular/forms'; import { CarouselComponent } from '../../../shared/carousel/carousel.component'; import { MatFormFieldModule } from '@angular/material/form-field'; import { MatInputModule } from '@angular/material/input'; import { MatButtonModule } from '@angular/material/button'; import { MatIconModule } from '@angular/material/icon'; import { MatCheckboxModule } from '@angular/material/checkbox'; import { Router, RouterLink } from '@angular/router'; import { AuthService } from 'src/app/_helpers/services/auth.service'; @Component({ selector: 'bgui-login-standard-user', standalone: true, imports: [ CommonModule, CarouselComponent, MatFormFieldModule, ReactiveFormsModule, MatInputModule, MatButtonModule, MatIconModule, MatCheckboxModule, RouterLink, FormsModule, ], templateUrl: './login-standard-user.component.html', styleUrls: ['./login-standard-user.component.scss'], providers: [AuthService], }) export class LoginStandardUserComponent implements OnInit { router = inject(Router); // eslint-disable-next-line @typescript-eslint/no-explicit-any //authService: any; constructor(private authService: AuthService) {} images = [ { imageSrc: './assets/images/carousel-auth/orange-with-variant.png', imageAlt: 'nature1', }, { imageSrc: './assets/images/carousel-auth/silver-modern-with-variant.png', imageAlt: 'nature1', }, ]; username = new FormControl('', [Validators.required]); password = new FormControl('', [Validators.required]); user: any = { username: '', password: '', }; getErrorMessage() { if (this.username.hasError('required')) { return 'Impossible de trouver votre compte Beasy'; } return this.username.hasError('email') ? 'Identifiant non valide' : ''; } getErrorAuth() { this.msgAuth = ''; } hide = true; isActive = false; msgAuth = ''; // eslint-disable-next-line @angular-eslint/no-empty-lifecycle-method ngOnInit() { this.user = { username: '', password: '', }; } login() { console.log('Username:', this.user); this.authService.login({ username: this.user.username, password: this.user.password, }); let token: string; console.log('Token ==> ', this.authService.token); if (this.authService.token) { token = this.authService.token; } else { const localStorageToken = localStorage.getItem('token'); token = localStorageToken ? String(localStorageToken) : ''; } console.log('Token ==> ', token); if (token) { this.authService.verifyToken(token).subscribe(result => { if (result.status) { console.log('Token vérifié avec succès'); console.log('accessToken', token); localStorage.setItem('isLoggedIn', 'true'); localStorage.setItem('token', token); localStorage.setItem('username', this.user.username); // Remplacez 'dash' par l'URL de la page à laquelle vous souhaitez rediriger console.log("Redirection vers 'dash'"); this.router.navigate(['/admin']); } else { console.error('Erreur lors de la vérification du token'); } }); } else { console.log('Token non trouvé'); } } refreshToken() { this.authService.refreshToken(); } logout() { this.authService.logout(); } onSubmit() {} }