Guest User

Untitled

a guest
May 1st, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. import { Component, OnInit } from '@angular/core';
  2. import {FormBuilder, FormGroup, Validators} from '@angular/forms';
  3. import {Router} from '@angular/router';
  4. import {AuthService} from "../../shared/authentication.service";
  5.  
  6.  
  7. interface Response {
  8. response: string;
  9. result: {
  10. token: string;
  11. };
  12. }
  13.  
  14. @Component({
  15. selector: 'bs-login',
  16. templateUrl: './login.component.html'
  17. })
  18. export class LoginComponent implements OnInit {
  19.  
  20. loginForm: FormGroup;
  21.  
  22. constructor(private fb: FormBuilder, private router: Router,
  23. private authService: AuthService ) { }
  24.  
  25. ngOnInit() {
  26. this.loginForm = this.fb.group({
  27. username: ['', [Validators.required, Validators.email]],
  28. password: ['', Validators.required],
  29. });
  30. }
  31.  
  32. login() {
  33. const val = this.loginForm.value;
  34. if (val.username && val.password) {
  35. this.authService.login(val.username, val.password).subscribe(res => {
  36. const resObj = res as Response;
  37. if (resObj.response === "success") {
  38. this.authService.setLocalStorage(resObj.result.token);
  39. this.router.navigateByUrl('/');
  40. }
  41. });
  42. }
  43. }
  44.  
  45.  
  46. isLoggedIn(){
  47. return this.authService.isLoggedIn();
  48. }
  49.  
  50. logout(){
  51. this.authService.logout();
  52. }
  53. }
Add Comment
Please, Sign In to add comment