Advertisement
Guest User

9

a guest
Jan 21st, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. import { Component, OnInit } from '@angular/core';
  2. import { FormBuilder, FormGroup, Validators } from '@angular/forms';
  3. import { AuthService } from '../auth/auth.service';
  4. import { catchError } from 'rxjs/operators';
  5. import { Observable, throwError } from 'rxjs';
  6. import { Router } from '@angular/router';
  7. import { HttpErrorResponse } from '@angular/common/http';
  8.  
  9. @Component({
  10. selector: 'app-login',
  11. templateUrl: './login.component.html',
  12. styleUrls: ['./login.component.scss']
  13. })
  14. export class LoginComponent implements OnInit {
  15. loginForm: FormGroup;
  16.  
  17. constructor(
  18. private formBuilder: FormBuilder,
  19. private authService: AuthService,
  20. private router: Router
  21. ) {
  22. this.loginForm = formBuilder.group ({
  23. username: ['', Validators.required],
  24. password: ['', Validators.required]
  25. });
  26. }
  27.  
  28. ngOnInit() {
  29. }
  30.  
  31. login(){
  32. this.authService.login(
  33. this.loginForm.get('username').value,
  34. this.loginForm.get('password').value
  35. ).pipe(
  36. catchError(
  37. (err: HttpErrorResponse, caught: Observable<Boolean>) => {
  38. console.log(`ERROR while login: ${err}`);
  39. return throwError(err);
  40.  
  41. }
  42. )
  43. ).subscribe(response => {
  44. if (response) {
  45. // login successfull...navigate to main page
  46. this.router.navigateByUrl('/main');
  47. }
  48. });
  49. }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement