Added alert
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user