Guest User

Untitled

a guest
Jan 11th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import { AngularFireAuth } from 'angularfire2/auth';
  4. import { AngularFireDatabase } from 'angularfire2/database';
  5. import * as firebase from 'firebase/app';
  6. import { Observable } from 'rxjs/Observable';
  7.  
  8. @Injectable({
  9. providedIn: 'root'
  10. })
  11. export class AuthService {
  12. private user: Observable<firebase.User>;
  13. private authState: any;
  14. userId: string;
  15. signupAttempt: number;
  16. userDetails: any;
  17.  
  18. constructor(private afAuth: AngularFireAuth,
  19. private db: AngularFireDatabase,
  20. private router: Router) {
  21. this.user = afAuth.authState;
  22. }
  23.  
  24. authenticateUser() {
  25. return this.user;
  26. }
  27.  
  28. getUserDetails(uid: string) {
  29. const path = `users/${uid}`;
  30. return this.db.object(path);
  31. }
  32.  
  33. login(email: string, password: string) {
  34. return this.afAuth.auth.signInWithEmailAndPassword(email, password)
  35. .then((user) => {
  36. this.authState = user;
  37. this.signupAttempt = 1;
  38. this.setUserSatus('online');
  39. this.getUserDetails(user.uid);
  40. this.router.navigate(['chat']);
  41. window.location.reload();
  42. });
  43. }
  44.  
  45. get currentUserId(): string {
  46. if (this.signupAttempt === 1) {
  47. this.signupAttempt = 0;
  48. return this.authState !== null ? this.authState.uid : ' ';
  49. } else {
  50. this.userId = sessionStorage.getItem('userId');
  51. return this.authState !== null ? this.userId : this.authState.uid;
  52. }
  53. }
  54.  
  55. setUserSatus(status: string) {
  56. const path = `users/${this.currentUserId}`;
  57. const data = {
  58. status: status
  59. };
  60. this.db.object(path).update(data);
  61. }
  62. }
Add Comment
Please, Sign In to add comment