Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. constructor(@Inject(AuthenticationService) private authSvc: AuthenticationService){
  2. this.authSvc.getAuthChangeObservable().subscribe(data => this.onAuthenticationChanged(data) );
  3. }
  4.  
  5. private authChange: Observable<any>;
  6. private _authObserver: Observer<any>;
  7.  
  8. constructor(@Inject(FirebaseService) firebaseService: FirebaseService){
  9.  
  10. // Cookies or whatever allow .auth() to identify the user after a page refresh
  11. // This causes a slight delay, see my question below
  12. firebase.auth().onAuthStateChanged(user => {
  13. if(user) this.onAuthenticationSuccessfull(user);
  14. else this.onLogoutSuccessfull();
  15. });
  16.  
  17. // Create an observable for auth status
  18. this.authChange = new Observable(observer => {
  19. this._authObserver = observer;
  20. }).share();
  21. }
  22.  
  23. getAuthChangeObservable(){
  24. return this.authChange;
  25. }
  26.  
  27. ...
  28.  
  29. onAuthenticationSuccessfull(user){
  30. this.authenticatedUser = user;
  31.  
  32. // This is mainly the way I found to warn other services they are clear to load the
  33. // user-related data
  34. this._authObserver.next('auth');
  35. }
  36. ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement