Guest User

Untitled

a guest
Dec 9th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 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-deprecated';
  5. import * as firebase from 'firebase/app';
  6. import { Observable } from 'rxjs/Observable';
  7. import { User } from '../models/user.model';
  8.  
  9.  
  10. @Injectable()
  11. export class AuthService {
  12.  
  13. private user: Observable<firebase.User>;
  14. private authState: any; /*observable to determine the authentication state of a current user*/
  15.  
  16. constructor(private afAuth: AngularFireAuth, private db: AngularFireDatabase, private router: Router) {
  17.  
  18. this.user = afAuth.authState;
  19.  
  20. }
  21.  
  22. setUserData(email: string, displayName: string, status: string): void {
  23. const path = `users/${this.currentUserId}`; /*a separate parent node for all of the users in the db*/
  24. const data = {
  25. email: email,
  26. displayName: displayName,
  27. status: status
  28. };
  29.  
  30. this.db.object(path).update(data)
  31. .catch(error => console.log(error));
  32. }
  33.  
  34. get currentUserId(): string {
  35. return this.authState !== null ? this.authState.uid : '';
  36. }
  37.  
  38. setUserStatus(status: string): void {
  39. const path = `users/${this.currentUserId}`;
  40.  
  41. const data = {
  42. status: status
  43. };
  44.  
  45. this.db.object(path).update(data)
  46. .catch(error => console.log(error));
  47. }
  48.  
  49. /* authUser() {
  50. return this.user;
  51. }*/
  52.  
  53.  
  54. signUp(email: string, password: string, displayName: string) { /*GATEWAY method*/
  55. return this.afAuth.auth.createUserWithEmailAndPassword(email, password)
  56. .then((user) => {
  57. this.authState = user;
  58. const status = 'online';
  59. this.setUserData(email, displayName, status); /*store the user's email, displayname, and status in users node at the time of registration*/
  60. }).catch(error => console.log(error));
  61. }
  62.  
  63.  
  64. }
  65.  
  66. TypeError: Cannot read property 'uid' of undefined
  67. at ChatService.getUser (chat.service.ts:35)
  68. at SafeSubscriber.eval [as _next] (chat.service.ts:26)
  69. at SafeSubscriber.__tryOrUnsub (Subscriber.js:239)
  70. at SafeSubscriber.next (Subscriber.js:186)
  71. at Subscriber._next (Subscriber.js:127)
  72. at Subscriber.next (Subscriber.js:91)
  73. at Notification.observe (Notification.js:32)
  74. at QueueAction.ObserveOnSubscriber.dispatch (observeOn.js:95)
  75. at QueueAction.AsyncAction._execute (AsyncAction.js:119)
  76. at QueueAction.execute (QueueAction.js:37)
  77.  
  78. @Injectable()
  79. export class ChatService {
  80. user: firebase.User;
  81. chatMessages: FirebaseListObservable<ChatMessage[]>;
  82. chatMessage: ChatMessage;
  83. userName: Observable<string>;
  84.  
  85. constructor(
  86. private db: AngularFireDatabase,
  87. private afAuth: AngularFireAuth
  88. ) {
  89. this.afAuth.authState.subscribe(auth => {
  90. if (auth !== undefined && auth !== null) {
  91. this.user = auth; // the user object is only going to be set if we are authenticated
  92. }
  93.  
  94. this.getUser().subscribe(a => {
  95. this.userName = a.displayName;
  96. });
  97.  
  98.  
  99. });
  100. }
  101.  
  102. getUser() { /*querying for the user id in the users node, which will contain the display name*/
  103. const userId = this.user.uid;
  104. const path = `/users/${userId}`;
  105. return this.db.object(path); /*this will return an observable that we can subscribe to*/
  106. }
Add Comment
Please, Sign In to add comment