Guest User

Untitled

a guest
Nov 18th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. import { Component, OnInit } from '@angular/core';
  2. import { FormGroup, FormControl } from '@angular/forms';
  3. import { Router } from '@angular/router';
  4.  
  5. import { OidcService } from '../services/oidc.service';
  6. import { TokenModel } from '../models/token.model';
  7. import { CanActivateGuard } from '../services/can-activate.service';
  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. public loginFormGroup: FormGroup;
  16. public isLoading: boolean;
  17. public loginError: boolean;
  18. public isLoggedIn: boolean;
  19.  
  20. constructor(
  21. public oidc: OidcService,
  22. public router: Router,
  23. public activateGuard: CanActivateGuard
  24. ) {
  25. this.loginError = false;
  26. if (this.checkLoginStatus()) {
  27. this.router.navigate(['/crm/']);
  28. }
  29. }
  30.  
  31. public checkLoginStatus() {
  32. if (localStorage.access_token) {
  33. return true;
  34. } else {
  35. return false;
  36. }
  37. }
  38.  
  39. public submitLoginForm(loginFormGroup: FormGroup) {
  40. if (!loginFormGroup) { return; }
  41. this.isLoading = true;
  42. this.oidc.postForKey(loginFormGroup.value.username, loginFormGroup.value.password)
  43. .subscribe((tokenObject: TokenModel) => {
  44. this.activateGuard.setAllowance(false);
  45. localStorage.setItem('access_token', tokenObject.access_token);
  46. location.reload();
  47. this.router.navigate(['/crm/']);
  48. }, error => {
  49. this.loginError = true;
  50. this.isLoading = false;
  51. });
  52. }
  53.  
  54. ngOnInit() {
  55. this.loginFormGroup = new FormGroup({
  56. username: new FormControl(''),
  57. password: new FormControl('')
  58. });
  59. }
  60. }
Add Comment
Please, Sign In to add comment