From 2d5c4f126e8f317aa6a533259f3b85b85b60d5fa Mon Sep 17 00:00:00 2001 From: "hugo.pradier2" Date: Mon, 17 Jun 2024 09:01:33 +0200 Subject: [PATCH] correctifs --- angular.json | 4 +- src/app/components/login/login.component.html | 46 +++++-------- src/app/components/login/login.component.ts | 50 +++++++------- .../register/register.component.html | 37 ++++------ .../components/register/register.component.ts | 69 ++++++++----------- src/app/services/user.service.ts | 25 ++++--- 6 files changed, 103 insertions(+), 128 deletions(-) diff --git a/angular.json b/angular.json index 21f2bb7..7d84a59 100644 --- a/angular.json +++ b/angular.json @@ -30,8 +30,8 @@ "budgets": [ { "type": "initial", - "maximumWarning": "500kb", - "maximumError": "1mb" + "maximumWarning": "2mb", + "maximumError": "3mb" }, { "type": "anyComponentStyle", diff --git a/src/app/components/login/login.component.html b/src/app/components/login/login.component.html index 23dd3c1..ef40f05 100644 --- a/src/app/components/login/login.component.html +++ b/src/app/components/login/login.component.html @@ -1,43 +1,33 @@ -
-

Formulaire de connexion :

- - - Enter your login - - +
+ + Login + + + Login is required + - - Enter your password + + Password - {{ errorMessage }} + + Password is required + - + - -
- {{ successLogin }} -
-
- {{ errorLogin }} -
-
+
{{ errorLogin }}
+
{{ successLogin }}
+ diff --git a/src/app/components/login/login.component.ts b/src/app/components/login/login.component.ts index 92857b8..4e8dc92 100644 --- a/src/app/components/login/login.component.ts +++ b/src/app/components/login/login.component.ts @@ -6,6 +6,8 @@ import { FormsModule, ReactiveFormsModule, NgForm, + FormGroup, + FormBuilder, } from '@angular/forms'; import { MatInputModule } from '@angular/material/input'; import { MatFormFieldModule } from '@angular/material/form-field'; @@ -34,41 +36,37 @@ import { User } from 'src/app/models/user.model'; export class LoginComponent { hide = true; - email = new FormControl('', [Validators.required, Validators.email]); - - login = new FormControl('', [Validators.required]); - - password = new FormControl('', [Validators.required]); + loginForm = this.formBuilder.group({ + login: ['', Validators.required], + password: ['', Validators.required], + }); errorMessage = ''; successLogin = ''; errorLogin = ''; - constructor(private userService: UserService) { - merge(this.email.statusChanges, this.email.valueChanges).pipe( - takeUntilDestroyed() - ); + constructor( + private userService: UserService, + private formBuilder: FormBuilder + ) { + this.userService = userService; } loginAction() { - console.log('login user :', this.login.value); + const formValue = this.loginForm.value; - const form: any = { - value: { - login: this.login.value, - password: this.password.value, - }, - }; - this.userService.loginUser(form).subscribe((response) => { - console.log('response :', response); - if ((response as any).success) { - this.successLogin = 'Vous êtes connecté.'; - this.errorLogin = ''; - } else { - this.errorLogin = "L'authentification a échoué."; - this.successLogin = ''; - } - }); + this.userService + .loginUser(formValue.login!, formValue.password!) + .subscribe((response) => { + console.log('response :', response); + if (response.success) { + this.successLogin = 'Vous êtes connecté.'; + this.errorLogin = ''; + } else { + this.errorLogin = "L'authentification a échoué."; + this.successLogin = ''; + } + }); } } diff --git a/src/app/components/register/register.component.html b/src/app/components/register/register.component.html index f76e7e7..1c3bf41 100644 --- a/src/app/components/register/register.component.html +++ b/src/app/components/register/register.component.html @@ -1,4 +1,4 @@ -
+

Formulaire d'inscription :

@@ -6,21 +6,19 @@ - {{ errorMessage }} + {{ + errorMessage + }} Enter your login - - {{ errorMessage }} + + {{ + errorMessage + }} @@ -28,8 +26,7 @@ - {{ errorMessage }} + {{ + errorMessage + }} - +
@@ -57,4 +50,4 @@
{{ errorRegister }}
-
+
diff --git a/src/app/components/register/register.component.ts b/src/app/components/register/register.component.ts index 4ccf2dc..29dcf19 100644 --- a/src/app/components/register/register.component.ts +++ b/src/app/components/register/register.component.ts @@ -6,6 +6,7 @@ import { FormsModule, ReactiveFormsModule, NgForm, + FormBuilder, } from '@angular/forms'; import { MatInputModule } from '@angular/material/input'; import { MatFormFieldModule } from '@angular/material/form-field'; @@ -34,54 +35,42 @@ import { User } from 'src/app/models/user.model'; export class RegisterComponent { hide = true; - email = new FormControl('', [Validators.required, Validators.email]); - - login = new FormControl('', [Validators.required]); - - password = new FormControl('', [Validators.required]); - + registerForm = this.formBuilder.group({ + email: ['', Validators.required], + login: ['', Validators.required], + password: ['', Validators.required], + }); errorMessage = ''; successRegister = ''; errorRegister = ''; - constructor(private userService: UserService) { - merge(this.email.statusChanges, this.email.valueChanges) - .pipe(takeUntilDestroyed()) - .subscribe(() => this.updateErrorMessage()); - } - - updateErrorMessage() { - if (this.email.hasError('required')) { - this.errorMessage = 'You must enter a value'; - } else if (this.email.hasError('email')) { - this.errorMessage = 'Not a valid email'; - } else if (this.login.hasError('required')) { - this.errorMessage = 'You must enter a login'; - } else { - this.errorMessage = ''; - } + constructor( + private userService: UserService, + private formBuilder: FormBuilder + ) { + this.userService = userService; } register() { - console.log('registering user :', this.login.value); + const formRegisterValue = this.registerForm.value; - const form: any = { - value: { - login: this.login.value, - password: this.password.value, - }, - }; - this.userService.postUser(form).subscribe((response) => { - console.log('response :', response); - if ((response as any).success) { - this.successRegister = 'Votre compte a été créé avec succès.'; - this.errorRegister = ''; - } else { - this.errorRegister = - "L'inscription a échoué : un compte avec ce login existe déjà."; - this.successRegister = ''; - } - }); + this.userService + .postUser( + formRegisterValue.email!, + formRegisterValue.login!, + formRegisterValue.password! + ) + .subscribe((response) => { + console.log('response :', response); + if (response.success) { + this.successRegister = 'Votre compte a été créé avec succès.'; + this.errorRegister = ''; + } else { + this.errorRegister = + "L'inscription a échoué : un compte avec ce login existe déjà."; + this.successRegister = ''; + } + }); } } diff --git a/src/app/services/user.service.ts b/src/app/services/user.service.ts index b35601a..7d20b18 100644 --- a/src/app/services/user.service.ts +++ b/src/app/services/user.service.ts @@ -12,23 +12,28 @@ export class UserService { constructor(private http: HttpClient) {} - postUser(form: NgForm): Observable> { - let body = { - login: form.value.login, - password: form.value.password, + postUser( + email: string, + login: string, + password: string + ): Observable { + const body = { + email: email, + login: login, + password: password, permissions: 0, }; - return this.http.post(`${this.API_URL}/users`, body); + return this.http.post(`${this.API_URL}/users`, body); } - loginUser(form: NgForm): Observable> { - let body = { - login: form.value.login, - password: form.value.password, + loginUser(login: string, password: string): Observable { + const body = { + login: login, + password: password, }; - return this.http.post(`${this.API_URL}/users/login`, body); + return this.http.post(`${this.API_URL}/users/login`, body); } }