From 13ad5d369cfd28fdf8964fc2c31280defeae1715 Mon Sep 17 00:00:00 2001 From: sanchezvem Date: Wed, 25 Mar 2026 14:07:24 +0100 Subject: [PATCH] Added alert --- src/app/pages/login/login.component.html | 10 ++--- src/app/pages/login/login.component.ts | 47 +++++++++++++++++------- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/app/pages/login/login.component.html b/src/app/pages/login/login.component.html index 37d504e..3f8ec80 100644 --- a/src/app/pages/login/login.component.html +++ b/src/app/pages/login/login.component.html @@ -3,17 +3,13 @@

Prêt à défier tes amis et prouver que t’es le meilleur ? - +

@if (authState()) { - @if (errorMessage()) { -

{{ errorMessage() }}

- } - - +

Se connecter

@@ -26,7 +22,7 @@ } - +

Créer un compte

\ No newline at end of file diff --git a/src/app/pages/login/login.component.ts b/src/app/pages/login/login.component.ts index ec0df97..8da905b 100644 --- a/src/app/pages/login/login.component.ts +++ b/src/app/pages/login/login.component.ts @@ -1,11 +1,10 @@ import {Component, inject, signal, viewChild} from '@angular/core'; -import {IonicModule} from "@ionic/angular"; +import {IonicModule, NavController, ToastController} from "@ionic/angular"; import {SignInFormComponent} from "../../components/sign-in-form/sign-in-form.component"; import {SignOnFormComponent} from "../../components/sign-on-form/sign-on-form.component"; import {AuthManageService} from "../../services/auth-manage"; import {firstValueFrom} from "rxjs"; import {UsersService} from "../../services/api"; -import {Router} from "@angular/router"; @Component({ selector: 'app-login', @@ -25,7 +24,8 @@ export class LoginComponent { private authManageService = inject(AuthManageService); private usersService = inject(UsersService); - private router = inject(Router); + private navCtrl = inject(NavController); + private toastCtrl = inject(ToastController); async createAccount() { if (this.authState()) { @@ -33,6 +33,12 @@ export class LoginComponent { } else if (this.authState() == false) { if (await this.addUser()) { this.authState.set(true); + const toast = await this.toastCtrl.create({ + message: 'Utilisateur crée', + duration: 2000, + color: 'success' + }); + await toast.present(); } } } @@ -44,36 +50,49 @@ export class LoginComponent { try { await this.authManageService.connectUser(user.username, user.password); - await this.router.navigate(['/home']); + await this.navCtrl.navigateRoot('/home'); } catch (e) { - console.error('Connexion échouée', e); - if (e.status === 401) { - this.errorMessage.set("Mot de passe incorrect"); - } else if (e.status === 404) { - this.errorMessage.set("Utilisateur introuvable"); + this.errorMessage.set("Identifiants incorrects"); } else { this.errorMessage.set("Erreur de connexion"); } + const toast = await this.toastCtrl.create({ + message: this.errorMessage(), + duration: 2000, + color: 'danger' + }); + await toast.present(); + this.login().loginForm.reset(); } } async addUser() { if (this.user().userForm.invalid) { - console.log('Erreur d\'écriture dans le formulaire'); + const toast = await this.toastCtrl.create({ + message: 'Tout les champs sont requis', + duration: 2000, + color: 'danger' + }); + await toast.present(); + this.user().userForm.reset(); return false; } try { const users = this.user().userForm.getRawValue(); - await firstValueFrom(this.usersService.createUserEndpoint(users)) - console.log('Utilisateur crée : ' + users); + await firstValueFrom(this.usersService.createUserEndpoint(users)); return true; } catch (e) { - console.log(e); - return false + const toast = await this.toastCtrl.create({ + message: 'Email ou mot de passe incorrect', + duration: 2000, + color: 'danger' + }); + await toast.present(); } + } }