login.component.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { Component, OnInit } from '@angular/core';
  2. import { FormBuilder, FormGroup, Validators } from '@angular/forms';
  3. import { DomSanitizer } from "@angular/platform-browser";
  4. import { MatIconRegistry } from '@angular/material/icon';
  5. import { AuthService } from 'src/app/services/auth.service'
  6. const googleLogoURL = "../assets/img/social/google+.svg";
  7. const facebookLogoURL = "../assets/img/social/facebook.svg";
  8. import {MatSnackBar} from '@angular/material/snack-bar';
  9. import { Router } from '@angular/router';
  10. @Component({
  11. selector: 'app-login',
  12. templateUrl: './login.component.html',
  13. styleUrls: ['./login.component.scss']
  14. })
  15. export class LoginComponent implements OnInit {
  16. constructor( public router: Router, public snackBar: MatSnackBar, private authService : AuthService, private formBuilder: FormBuilder, private matIconRegistry: MatIconRegistry, private domSanitizer: DomSanitizer)
  17. { this.matIconRegistry.addSvgIcon("googleLogo", this.domSanitizer.bypassSecurityTrustResourceUrl(googleLogoURL)),
  18. this.matIconRegistry.addSvgIcon("facebookLogo", this.domSanitizer.bypassSecurityTrustResourceUrl(facebookLogoURL))}
  19. formGroup: FormGroup;
  20. submitted = false;
  21. loading = false;
  22. hide =true;
  23. ngOnInit(): void {
  24. this.createForm();
  25. }
  26. createForm() {
  27. 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,}))$/
  28. this.formGroup = this.formBuilder.group({
  29. email: [null, [Validators.required, Validators.pattern(emailregex)]],
  30. password: [null, [Validators.required, this.checkPassword]],
  31. });
  32. }
  33. checkPassword(control) {
  34. let enteredPassword = control.value
  35. let passwordCheck = /^(?=.*[a-z])(?=.{6,})/;
  36. return (!passwordCheck.test(enteredPassword) && enteredPassword) ? { 'requirements': true } : null;
  37. }
  38. getErrorEmail() {
  39. return this.formGroup.get('email').hasError('required') ? 'Adresse email requise' :
  40. this.formGroup.get('email').hasError('pattern') ? 'Adresse email non valide' : '';
  41. }
  42. getErrorPassword() {
  43. return this.formGroup.get('password').hasError('required') ? 'Mot de passe requis' :
  44. this.formGroup.get('password').hasError('requirements') ?
  45. 'Le mot de passe doit comporter au moins 8 caractères, une lettre majuscule' : '';
  46. }
  47. get f() { return this.formGroup.controls; }
  48. // login
  49. signIn(){
  50. this.submitted = true;
  51. if (this.formGroup.invalid) {
  52. return;
  53. }
  54. this.loading = true;
  55. this.authService.signIn(this.formGroup.value).subscribe(
  56. data => {
  57. // this.router.navigate([this.returnUrl]);
  58. console.log(data)
  59. this.loading = false;
  60. this.router.navigate(['/auth']);
  61. },
  62. err => {
  63. this.loading = false;
  64. console.log(err.error.message)
  65. this.authService.openSnackBar(err.error.message)
  66. });
  67. // this.router.navigate(['/']);
  68. }
  69. }