Advertisement
Guest User

Untitled

a guest
Jul 31st, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. import { Component, OnDestroy, OnInit } from '@angular/core';
  2. import { FormBuilder, FormGroup, Validators } from '@angular/forms';
  3. import { ENV } from '@app/env';
  4. import { Store } from '@ngrx/store';
  5. import { IonicPage, NavController, NavParams } from 'ionic-angular';
  6. import LogRocket from 'logrocket';
  7. import { Observable } from 'rxjs/Observable';
  8. import { Subscription } from 'rxjs/Subscription';
  9.  
  10. import { AuthenticateAction, LoadAuthenticatedUserAction } from '../../app/session/session.actions';
  11. import * as fromSession from '../../app/session/session.reducer';
  12. import { User } from '../../app/session/user';
  13. import * as fromLoadingIndicatiorActions from './../../app/loading-indicator/loading-indicator.actions';
  14. import * as fromToastActions from './../../app//toast/toast.actions';
  15.  
  16.  
  17. /**
  18. * Generated class for the LoginPage page.
  19. *
  20. * See https://ionicframework.com/docs/components/#navigation for more info on
  21. * Ionic pages and navigation.
  22. */
  23. @IonicPage()
  24. @Component({
  25. selector: "page-login",
  26. templateUrl: "login.html"
  27. })
  28. export class LoginPage implements OnInit, OnDestroy{
  29.  
  30. public form: FormGroup;
  31. public autenticated$: Observable<boolean>;
  32. public _authenticatedSub: Subscription;
  33. public user$: Observable<User>;
  34. public error: string;
  35. public errorSub: Subscription;
  36.  
  37. constructor(
  38. public navCtrl: NavController,
  39. public navParams: NavParams,
  40. private store: Store<fromSession.State>,
  41. private formBuilder: FormBuilder,
  42. ) {
  43. }
  44.  
  45. public ngOnInit() {
  46. // create the login form
  47. this.form = this.formBuilder.group({
  48. username: ["", Validators.required],
  49. password: ["", Validators.required]
  50. });
  51.  
  52. // get the authenticated observable from the store
  53. this.autenticated$ = this.store.select(fromSession.isAuthenticated);
  54. this.user$ = this.store.select(fromSession.currentUser);
  55.  
  56. // subscribe to a succesfull authentication
  57. this._authenticatedSub = this.autenticated$.subscribe(value => {
  58. if (value) {
  59. this.onAuthenticationSuccess();
  60. }
  61. });
  62.  
  63. this.user$.subscribe(value => {
  64. if (value) {
  65. if (ENV.mode === "Production") {
  66. LogRocket.identify(value.email, {
  67. name: value.firstName,
  68. lastName: value.lastName,
  69. email: value.email
  70. });
  71. }
  72. Bugfender.setDeviceKey('email', 'test@example.com');
  73. }
  74. });
  75.  
  76. this.errorSub = this.store.select(fromSession.selectError).subscribe(error => {
  77. if (error) {
  78. if (error.error && error.error.error && error.error.error.detail === 'Bad credentials') {
  79. this.error = 'Passwort und oder Benutzername falsch.'
  80. } else {
  81. this.store.dispatch(new fromToastActions.ShowToastAction(''));
  82. }
  83. this.store.dispatch(
  84. new fromLoadingIndicatiorActions.DismissLoadingIndicatorAction()
  85. );
  86. } else {
  87. this.error = undefined;
  88. }
  89. })
  90.  
  91. }
  92.  
  93. public ngOnDestroy() {
  94. this._authenticatedSub.unsubscribe();
  95. this.errorSub.unsubscribe();
  96. }
  97.  
  98. /**
  99. * Submits the login form.
  100. *
  101. */
  102. public submit() {
  103. // get email and password values
  104. const username: string = this.form.get("username").value;
  105. const password: string = this.form.get("password").value;
  106.  
  107. // trim values
  108. username.trim();
  109. password.trim();
  110.  
  111. this.store.dispatch(
  112. new fromLoadingIndicatiorActions.ShowLoadingIndicatorAction()
  113. );
  114. // dispatch the action
  115. this.store.dispatch(new AuthenticateAction(username, password));
  116. }
  117.  
  118. /**
  119. * Dispatch the action to load the current user object,
  120. * redirect to the TourPage and set the TourPage as root
  121. * (its not necessary to return to the login page).
  122. *
  123. */
  124. public onAuthenticationSuccess() {
  125. this.store.dispatch(new LoadAuthenticatedUserAction());
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement