register.component.ts 6.4 KB

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