Guest User

Untitled

a guest
Feb 12th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. import { Component, ViewEncapsulation } from '@angular/core';
  2. import { FormGroup, AbstractControl, FormBuilder, Validators } from '@angular/forms';
  3. import { Router } from '@angular/router';
  4. import { AuthService } from '../../auth.service';
  5.  
  6. @Component({
  7. selector: 'login',
  8. encapsulation: ViewEncapsulation.None,
  9. styles: [require('./login.scss')],
  10. template: require('./login.html')
  11. })
  12.  
  13. export class Login {
  14. public form:FormGroup;
  15. public username:AbstractControl;
  16. public password:AbstractControl;
  17. public submitted:boolean = false;
  18. private isRequesting:boolean = false;
  19. error;
  20. constructor(fb:FormBuilder,private router: Router, public authService:AuthService) {
  21. this.form = fb.group({
  22. 'username': ['', Validators.compose([Validators.required, Validators.minLength(1)])],
  23. 'password': ['', Validators.compose([Validators.required, Validators.minLength(1)])]
  24. });
  25.  
  26. this.username = this.form.controls['username'];
  27. this.password = this.form.controls['password'];
  28.  
  29. this.username.valueChanges.subscribe(value=>{
  30. this.error = null;
  31. })
  32. this.password.valueChanges.subscribe(value=>{
  33. this.error = null;
  34. })
  35.  
  36. }
  37. ngOnInit(){}
  38. public onSubmit(values:Object):void {
  39. this.submitted = true;
  40. this.isRequesting = true;
  41. if (this.form.valid) {
  42. values['grant_type'] ='password';
  43. this.authService.doLogin(values).then(response=>{
  44. let loginData = response.json();
  45. this.authService.setLoggedIn(`${loginData.token_type} ${loginData.access_token}`);
  46. this.authService.setRefreshToken(`${loginData.refresh_token}`);
  47. this.authService.setUserId(`${loginData.user_id}`);
  48. localStorage.setItem('logindata',`${loginData.token_type} ${loginData.access_token}`);
  49. localStorage.setItem('user_id',`${loginData.user_id}`);
  50. localStorage.setItem('refresh_token',`${loginData.refresh_token}`);
  51. this.router.navigate(['/pages']);
  52. }).catch(err=>{
  53. this.isRequesting = false;
  54. if(err.status === 0 && err.statusText === "") {
  55. this.error = err;
  56. this.error.Message = "There is no Internet connection or Server is unreachable";
  57. }else{
  58. this.error = err.json();
  59. }
  60. })
  61. }
  62. }
  63. }
Add Comment
Please, Sign In to add comment