Advertisement
Guest User

Untitled

a guest
May 7th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. import { Observable } from 'rxjs/Observable';
  2. import { AuthenticationService } from '../authentication.service';
  3. import { Component, OnInit } from '@angular/core';
  4. import { Router } from '@angular/router';
  5. import {
  6. AbstractControl,
  7. FormBuilder,
  8. FormGroup,
  9. ValidatorFn,
  10. Validators,
  11. FormControl
  12. } from '@angular/forms';
  13. import { HttpErrorResponse } from '@angular/common/http';
  14. import { map } from 'rxjs/operators';
  15.  
  16. function passwordValidator(length: number): ValidatorFn {
  17. return (control: AbstractControl): { [key: string]: any } => {
  18. return control.value.length < length
  19. ? {
  20. passwordTooShort: {
  21. requiredLength: length,
  22. actualLength: control.value.length
  23. }
  24. }
  25. : null;
  26. };
  27. }
  28.  
  29. function comparePasswords(control: AbstractControl): { [key: string]: any } {
  30. const password = control.get('password');
  31. const confirmPassword = control.get('confirmPassword');
  32. return password.value === confirmPassword.value
  33. ? null
  34. : { passwordsDiffer: true };
  35. }
  36.  
  37. @Component({
  38. selector: 'app-register',
  39. templateUrl: './register.component.html',
  40. styleUrls: ['./register.component.css']
  41. })
  42. export class RegisterComponent implements OnInit {
  43. public user: FormGroup;
  44. public errorMsg: string;
  45.  
  46. get passwordControl(): FormControl {
  47. return <FormControl>this.user.get('passwordGroup').get('password');
  48. }
  49.  
  50. constructor(
  51. private authenticationService: AuthenticationService,
  52. private router: Router,
  53. private fb: FormBuilder
  54. ) {}
  55.  
  56. ngOnInit() {
  57. this.user = this.fb.group({
  58. username: [
  59. '',
  60. [Validators.required, Validators.minLength(4)],
  61. this.serverSideValidateUsername()
  62. ],
  63. passwordGroup: this.fb.group(
  64. {
  65. password: ['', [Validators.required, passwordValidator(12)]],
  66. confirmPassword: ['', Validators.required]
  67. },
  68. { validator: comparePasswords }
  69. )
  70. });
  71. }
  72.  
  73. serverSideValidateUsername(): ValidatorFn {
  74. return (control: AbstractControl): Observable<{ [key: string]: any }> => {
  75. return this.authenticationService
  76. .checkUserNameAvailability(control.value)
  77. .pipe(
  78. map(available => {
  79. if (available) {
  80. return null;
  81. }
  82. return { userAlreadyExists: true };
  83. })
  84. );
  85. };
  86. }
  87.  
  88. onSubmit() {
  89. this.authenticationService
  90. .register(this.user.value.username, this.passwordControl.value)
  91. .subscribe(
  92. val => {
  93. if (val) {
  94. this.router.navigate(['/recipe/list']);
  95. }
  96. },
  97. (error: HttpErrorResponse) => {
  98. this.errorMsg = `Error ${
  99. error.status
  100. } while trying to register user ${this.user.value.username}: ${
  101. error.error
  102. }`;
  103. }
  104. );
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement