register.component.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import { Router } from '@angular/router';
  2. import { Component, OnInit } from '@angular/core';
  3. import { FormControl, FormGroupDirective, NgForm, Validators, FormGroup, FormBuilder } from '@angular/forms';
  4. import { HttpClient, HttpHeaders } from '@angular/common/http';
  5. import { ErrorStateMatcher } from '@angular/material/core';
  6. import { DomSanitizer } from "@angular/platform-browser";
  7. import { MatIconRegistry } from '@angular/material/icon';
  8. import { AuthService } from 'src/app/services/auth.service'
  9. import { MustMatch } from '../shared/validator/confirm-password.validator';
  10. const googleLogoURL = "../assets/img/social/google+.svg";
  11. const facebookLogoURL = "../assets/img/social/facebook.svg";
  12. declare const gapi: any;
  13. @Component({
  14. selector: 'app-register',
  15. templateUrl: './register.component.html',
  16. styleUrls: ['./register.component.scss']
  17. })
  18. export class RegisterComponent implements OnInit {
  19. http: any;
  20. customErrors: any;
  21. constructor(public router: Router, private authService : AuthService, private formBuilder: FormBuilder, private matIconRegistry: MatIconRegistry, private domSanitizer: DomSanitizer)
  22. { this.matIconRegistry.addSvgIcon("googleLogo", this.domSanitizer.bypassSecurityTrustResourceUrl(googleLogoURL)),
  23. this.matIconRegistry.addSvgIcon("facebookLogo", this.domSanitizer.bypassSecurityTrustResourceUrl(facebookLogoURL))
  24. const currentYear = new Date().getFullYear();
  25. this.maxDate = new Date(currentYear - 10, 11, 31);}
  26. maxDate: Date;
  27. formGroup: FormGroup;
  28. submitted = false;
  29. loading = false;
  30. hide =true;
  31. ngOnInit(): void {
  32. this.createForm();
  33. }
  34. createForm() {
  35. let emailregex: RegExp = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  36. let caracereregex : RegExp = /^[a-zA-ZàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųūÿýżźñçčšžÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ∂ð ,.'-]+$/;
  37. let adressregex : RegExp = /^[A-z0-9À-ž\s ,.'-]+$/;
  38. let phonenumberregex : RegExp = /^(?:(?:\+|00)33|0)\s*[1-9](?:[\s.-]*\d{2}){4}$/;
  39. let dateregex : RegExp = /^((31(?!\ (Feb(ruary)?|Apr(il)?|June?|(Sep(?=\b|t)t?|Nov)(ember)?)))|((30|29)(?!\ Feb(ruary)?))|(29(?=\ Feb(ruary)?\ (((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\d|2[0-8])\ (Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep(?=\b|t)t?|Nov|Dec)(ember)?)\ ((1[6-9]|[2-9]\d)\d{2})$/;
  40. this.formGroup = this.formBuilder.group({
  41. lastname: [null, [Validators.required, Validators.pattern(caracereregex)]],
  42. firstname: [null, [Validators.required, Validators.pattern(caracereregex)]],
  43. phonenumber: [null,[Validators.required, Validators.pattern(phonenumberregex)]],
  44. birthday: [null,[Validators.required]],
  45. terms: [false, Validators.requiredTrue],
  46. adress: [null,[Validators.required, Validators.pattern(adressregex)]],
  47. email: [null, [Validators.required, Validators.pattern(emailregex)]],
  48. password: [null, [Validators.required, this.checkPassword]],
  49. confirmPassword:[null, Validators.required]
  50. }, {
  51. validator: MustMatch('password', 'confirmPassword')
  52. });
  53. }
  54. checkPasswords(group: FormGroup) { // here we have the 'passwords' group
  55. let pass = group.controls.password.value;
  56. let confirmPass = group.controls.confirmPassword.value;
  57. return pass === confirmPass ? null : { notSame : true }
  58. }
  59. getErrorLastname() {
  60. return this.formGroup.get('lastname').hasError('required') ? 'Nom requis' :
  61. this.formGroup.get('lastname').hasError('pattern') ? 'Nom non valide' : 'Nom non valide';
  62. }
  63. getErrorFirstname() {
  64. return this.formGroup.get('firstname').hasError('required') ? 'Prénom requis' :
  65. this.formGroup.get('firstname').hasError('pattern') ? 'Prénom non valide' : 'Prénom non valide';
  66. }
  67. getErrorPhonenumber() {
  68. return this.formGroup.get('phonenumber').hasError('required') ? 'Numéro de téléphone requis' :
  69. this.formGroup.get('phonenumber').hasError('pattern') ? 'Numéro de téléphone non valide' : '';
  70. }
  71. getErrorAdress() {
  72. return this.formGroup.get('adress').hasError('required') ? 'adresse requise' :
  73. this.formGroup.get('adress').hasError('pattern') ? 'Prénom non valide' : 'Prénom non valide';
  74. }
  75. getErrorDate() {
  76. return this.formGroup.get('birthday').hasError('required') ? 'Date de naissance requise' :
  77. this.formGroup.get('birthday').hasError('pattern') ? 'Date de naissance non valide' : '';
  78. }
  79. getErrorTerms(){
  80. return this.formGroup.get('terms').hasError('required') ? 'Please accept the terms' :
  81. this.formGroup.get('terms').hasError('pattern') ? 'terms not accepted' : '';
  82. }
  83. getErrorConfirmPassword() {
  84. return this.formGroup.get('confirmPassword').hasError('required') ? 'Confirmation mot de passe requise' :
  85. this.formGroup.hasError('MustMatch') ?
  86. '' : 'Les mots de passe saisis ne sont pas identiques';
  87. }
  88. checkPassword(control) {
  89. let enteredPassword = control.value
  90. let passwordCheck = /^(?=.*[A-Z])(?=.*[a-z])(?=.{8,})/;
  91. return (!passwordCheck.test(enteredPassword) && enteredPassword) ? { 'requirements': true } : null;
  92. }
  93. getErrorEmail() {
  94. return this.formGroup.get('email').hasError('required') ? 'Adresse email requise' :
  95. this.formGroup.get('email').hasError('pattern') ? 'Adresse email non valide' : '';
  96. }
  97. getErrorPassword() {
  98. return this.formGroup.get('password').hasError('required') ? 'Mot de passe requis' :
  99. this.formGroup.get('password').hasError('requirements') ?
  100. 'Le mot de passe doit comporter au moins 8 caractères, une lettre majuscule, une lettre majuscule' : '';
  101. }
  102. get f() { return this.formGroup.controls; }
  103. // openSnackBar(message : string) {
  104. // this.snackBar.open(message, 'action', {
  105. // duration: 5000,
  106. // });
  107. // }
  108. //google signIn
  109. signInWithGoogle(): void {
  110. this.authService.signInWithGoogle().subscribe(
  111. _user => {
  112. // Redirection après l'authentification réussie avec Google
  113. this.router.navigate(['/auth']);
  114. },
  115. error => {
  116. console.log(error);
  117. // Gérer l'erreur
  118. }
  119. );
  120. }
  121. // this.authService.signGoogle().subscribe(
  122. // data=> {
  123. // console.log(data);
  124. // this.router.navigate(['/auth']);
  125. // },
  126. // error=> {
  127. // console.log(error);
  128. // });
  129. signUp(): void{
  130. this.submitted = true;
  131. if (this.formGroup.invalid) {
  132. return;
  133. }
  134. this.loading = true;
  135. this.authService.signUP(this.formGroup.value).subscribe(
  136. data => {
  137. console.log(data)
  138. this.loading = false;
  139. this.router.navigate(['/auth']);
  140. setTimeout(() => {
  141. document.location.reload()
  142. },2000)
  143. },
  144. err => {
  145. this.loading = false;
  146. console.log(err.error.message)
  147. this.authService.openSnackBar(err.error.message)
  148. });
  149. console.log(this.formGroup.value)
  150. }
  151. }