Advertisement
Guest User

Untitled

a guest
May 7th, 2018
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. import { AuthenticationService } from '../authentication.service';
  2. import { Component, OnInit } from '@angular/core';
  3. import { Router } from '@angular/router';
  4. import {
  5. AbstractControl,
  6. FormBuilder,
  7. FormGroup,
  8. ValidatorFn,
  9. Validators
  10. } from '@angular/forms';
  11. import { HttpErrorResponse } from '@angular/common/http';
  12.  
  13. function passwordValidator(): ValidatorFn {
  14. return (control: AbstractControl): { [key: string]: any } => {
  15. console.log(control.value);
  16. return control.value.length < 12
  17. ? { passwordTooShort: { value: control.value.length } }
  18. : null;
  19. };
  20. }
  21.  
  22. @Component({
  23. selector: 'app-login',
  24. templateUrl: './login.component.html',
  25. styleUrls: ['./login.component.css']
  26. })
  27. export class LoginComponent implements OnInit {
  28. public user: FormGroup;
  29. public errorMsg: string;
  30.  
  31. constructor(
  32. private authService: AuthenticationService,
  33. private router: Router,
  34. private fb: FormBuilder
  35. ) {}
  36.  
  37. ngOnInit() {
  38. this.user = this.fb.group({
  39. username: ['', Validators.required],
  40. password: ['', Validators.required]
  41. });
  42. }
  43.  
  44. onSubmit() {
  45. this.authService
  46. .login(this.user.value.username, this.user.value.password)
  47. .subscribe(
  48. val => {
  49. if (val) {
  50. if (this.authService.redirectUrl) {
  51. this.router.navigateByUrl(this.authService.redirectUrl);
  52. this.authService.redirectUrl = undefined;
  53. } else {
  54. this.router.navigate(['/recipe/list']);
  55. }
  56. } else {
  57. this.errorMsg = `Could not login`;
  58. }
  59. },
  60. (err: HttpErrorResponse) => {
  61. if (err.error instanceof Error) {
  62. this.errorMsg = `Error while trying to login user ${
  63. this.user.value.username
  64. }: ${err.error.message}`;
  65. } else {
  66. this.errorMsg = `Error ${err.status} while trying to login user ${
  67. this.user.value.username
  68. }: ${err.error}`;
  69. }
  70. }
  71. );
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement