Skip to content
Snippets Groups Projects
Select Git revision
  • 756f58ba03168773670d5cd2fe9b1b7387daad7d
  • master default protected
2 results

Model.sab.index

Blame
  • sign-up.component.ts 2.22 KiB
    import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
    import {FormControl, FormGroup, Validators} from "@angular/forms";
    import {HttpClient} from "@angular/common/http";
    import {ErrorLogin, LoginService} from "../login.service";
    
    @Component({
      selector: 'app-sign-up',
      templateUrl: './sign-up.component.html',
      styleUrls: ['./sign-up.component.css']
    })
    export class SignUpComponent{
      formData!: FormGroup;
      @ViewChild('passwordConfirmation') passwordConfirmation!: ElementRef
      errorOccured!: boolean;
      constructor(private httpClient: HttpClient, private loginService: LoginService) { }
    
      ngOnInit() {
        this.errorOccured=false;
        this.formData = new FormGroup({
          username: new FormControl(null, {
            updateOn: 'change',
            validators: [Validators.required],
          }),
          firstname: new FormControl(null, {
            updateOn: 'change',
            validators: [Validators.required],
          }),
          lastname: new FormControl(null, {
            updateOn: 'change',
            validators: [Validators.required],
          }),
          email: new FormControl(null, {
            updateOn: 'change',
            validators: [Validators.required],
          }),
          password: new FormControl(null, {
            updateOn: 'change',
            validators: [Validators.required],
          }),
          accountType: new FormControl(0)
        });
      }
    
    
    
      submitForm(event: Event) {
        event.preventDefault();
        this.errorOccured=false;
        if(this.formData.value.password !== this.passwordConfirmation.nativeElement.value){
          this.errorOccured=true;
          this.loginService.actualError=ErrorLogin.PassConfirm;
          console.log("mot de passe non identique");
          return;
        }
        this.formData.value.password = this.loginService.hashPassword(this.formData.value.password);
        this.httpClient.post<any>('http://localhost:30992/api/v1/guest/create-account', this.formData.value).subscribe(
          response => {
            console.log(response); // Affiche la réponse du serveur dans la console
            // Effectuer une action en fonction de la réponse du serveur ici
          },
          error => {
            this.errorOccured=true;
            if(error.error.message === "USER_EXIST"){
              this.loginService.actualError=ErrorLogin.UserNameExist;
            }
          }
        );
      }
    }