Guest User

Untitled

a guest
Jan 4th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 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.  
  22. signup(email: string, userName: string, password: string) {
  23. return this.afAuth.auth.createUserWithEmailAndPassword(email, password)
  24. .then((user) => {
  25. this.signupAttempt = 1;
  26. this.authState = user;
  27. const status = 'online';
  28. this.setUserData(email, userName, status);
  29. window.location.reload();
  30. });
  31. }
  32.  
  33. setUserData(email: string, userName: string, status: string) {
  34. const path = `users/${this.currentUserId}`;
  35. const data = {
  36. email: email,
  37. userName: userName,
  38. status: status,
  39. };
  40.  
  41. this.db.object(path).update(data);
  42. }
  43.  
  44. get currentUserId(): string {
  45. if (this.signupAttempt === 1) {
  46. this.signupAttempt = 0;
  47. return this.authState !== null ? this.authState.uid : ' ';
  48. } else {
  49. this.userId = sessionStorage.getItem('userId');
  50. return this.authState !== null ? this.userId : this.authState.uid;
  51. }
  52. }
  53. }
Add Comment
Please, Sign In to add comment