implemented routes and error messages in login page

This commit is contained in:
2026-03-24 23:55:53 +01:00
parent 6ec656fde1
commit 69c2991c54
19 changed files with 141 additions and 14 deletions
+30 -6
View File
@@ -5,6 +5,7 @@ import {SignOnFormComponent} from "../../components/sign-on-form/sign-on-form.co
import {AuthManageService} from "../../services/auth-manage";
import {firstValueFrom} from "rxjs";
import {UsersService} from "../../services/api";
import {Router} from "@angular/router";
@Component({
selector: 'app-login',
@@ -18,38 +19,61 @@ import {UsersService} from "../../services/api";
})
export class LoginComponent {
authState = signal<boolean>(true)
errorMessage = signal<string | null>(null);
login = viewChild<SignInFormComponent>('loginForm');
user = viewChild<SignOnFormComponent>('userForm');
private authManageService = inject(AuthManageService);
private usersService = inject(UsersService);
private router = inject(Router);
async createAccount() {
if (this.authState()) {
this.authState.set(false);
} else if (this.authState() == false) {
await this.addUser();
this.authState.set(true);
if (await this.addUser()) {
this.authState.set(true);
}
}
}
connectUser() {
async connectUser() {
const user = this.login().loginForm.getRawValue();
console.log(user);
this.authManageService.connectUser(user.username, user.password);
this.errorMessage.set(null);
try {
await this.authManageService.connectUser(user.username, user.password);
await this.router.navigate(['/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");
} else {
this.errorMessage.set("Erreur de connexion");
}
this.login().loginForm.reset();
}
}
async addUser() {
if (this.user().userForm.invalid) {
console.log('Erreur d\'écriture dans le formulaire');
return;
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);
return true;
} catch (e) {
console.log(e);
return false
}
}
}