Files
pmu-plateforme-jeux-admin-plr/src/app/auth/pages/login/login.ts
2025-12-16 14:20:02 +01:00

48 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Component, signal } from '@angular/core'; // Import OnInit
import { Validators, FormBuilder, FormGroup } from '@angular/forms'; // Import FormGroup
import { Router } from '@angular/router';
import { toast } from 'ngx-sonner';
import { Auth } from 'src/app/core/services/auth';
@Component({
selector: 'app-login',
templateUrl: './login.html',
styleUrl: './login.css',
standalone: false,
})
export class Login {
showPassword = false;
loading = signal(false);
errorMsg = signal('');
form!: FormGroup;
constructor(private fb: FormBuilder, private auth: Auth, private router: Router) {
this.form = this.fb.group({
identifiant: ['', [Validators.required]],
password: ['', [Validators.required, Validators.minLength(8)]],
});
}
async submit() {
this.errorMsg.set('');
if (this.form.invalid) {
this.form.markAllAsTouched();
return;
}
this.loading.set(true);
try {
const { identifiant, password } = this.form.value;
await this.auth.login(identifiant!, password!);
await this.router.navigateByUrl('/');
toast.success('Connexion réussie ! Bienvenue.');
} catch (e: any) {
this.errorMsg.set(
e?.message || e?.error?.message || 'Échec de connexion. Veuillez réessayer.'
);
toast.error(this.errorMsg(), { duration: 5000 });
} finally {
this.loading.set(false);
}
}
}