Guest User

Untitled

a guest
Jun 26th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. export class LoginPage implements OnInit {
  2.  
  3. loginForm: FormGroup;
  4. loading = false;
  5. submitted = false;
  6. returnUrl: string;
  7. error = '';
  8.  
  9. constructor(
  10. private formBuilder: FormBuilder,
  11. private route: ActivatedRoute,
  12. private router: Router,
  13. private authenticationService: AuthenticationService) {
  14. }
  15.  
  16. ngOnInit() {
  17. this.loginForm = this.formBuilder.group({
  18. username: ['', Validators.required],
  19. password: ['', Validators.required]
  20. });
  21.  
  22. // reset login status
  23. this.authenticationService.logout();
  24.  
  25. // get return url from route parameters or default to '/'
  26. this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
  27. }
  28.  
  29. /**
  30. * Convenience getter for easy access to form fields
  31. */
  32. get controls() {
  33. return this.loginForm.controls;
  34. }
  35.  
  36. onSubmit() {
  37. this.submitted = true;
  38.  
  39. // stop here if form is invalid
  40. if (this.loginForm.invalid) {
  41. return;
  42. }
  43.  
  44. this.loading = true;
  45. this.authenticationService
  46. .login(this.controls.username.value, this.controls.password.value)
  47. .pipe(first())
  48. .subscribe(
  49. data => {
  50. console.log('url where we go ' + this.returnUrl); //return the good url
  51. console.log(data);
  52. this.router.navigateByUrl(this.returnUrl, { replaceUrl: true })
  53. .then(function(res) {
  54. console.log(res); //return true
  55. });
  56. },
  57. error => {
  58. this.error = error;
  59. this.loading = false;
  60. console.log(this.error);
  61. });
  62. }
Add Comment
Please, Sign In to add comment