Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- import { Router } from '@angular/router';
- import { AngularFireAuth } from 'angularfire2/auth';
- import { AngularFireDatabase } from 'angularfire2/database-deprecated';
- import * as firebase from 'firebase/app';
- import { Observable } from 'rxjs/Observable';
- import { User } from '../models/user.model';
- @Injectable()
- export class AuthService {
- private user: Observable<firebase.User>;
- private authState: any; /*observable to determine the authentication state of a current user*/
- constructor(private afAuth: AngularFireAuth, private db: AngularFireDatabase, private router: Router) {
- this.user = afAuth.authState;
- }
- setUserData(email: string, displayName: string, status: string): void {
- const path = `users/${this.currentUserId}`; /*a separate parent node for all of the users in the db*/
- const data = {
- email: email,
- displayName: displayName,
- status: status
- };
- this.db.object(path).update(data)
- .catch(error => console.log(error));
- }
- get currentUserId(): string {
- return this.authState !== null ? this.authState.uid : '';
- }
- setUserStatus(status: string): void {
- const path = `users/${this.currentUserId}`;
- const data = {
- status: status
- };
- this.db.object(path).update(data)
- .catch(error => console.log(error));
- }
- /* authUser() {
- return this.user;
- }*/
- signUp(email: string, password: string, displayName: string) { /*GATEWAY method*/
- return this.afAuth.auth.createUserWithEmailAndPassword(email, password)
- .then((user) => {
- this.authState = user;
- const status = 'online';
- this.setUserData(email, displayName, status); /*store the user's email, displayname, and status in users node at the time of registration*/
- }).catch(error => console.log(error));
- }
- }
- TypeError: Cannot read property 'uid' of undefined
- at ChatService.getUser (chat.service.ts:35)
- at SafeSubscriber.eval [as _next] (chat.service.ts:26)
- at SafeSubscriber.__tryOrUnsub (Subscriber.js:239)
- at SafeSubscriber.next (Subscriber.js:186)
- at Subscriber._next (Subscriber.js:127)
- at Subscriber.next (Subscriber.js:91)
- at Notification.observe (Notification.js:32)
- at QueueAction.ObserveOnSubscriber.dispatch (observeOn.js:95)
- at QueueAction.AsyncAction._execute (AsyncAction.js:119)
- at QueueAction.execute (QueueAction.js:37)
- @Injectable()
- export class ChatService {
- user: firebase.User;
- chatMessages: FirebaseListObservable<ChatMessage[]>;
- chatMessage: ChatMessage;
- userName: Observable<string>;
- constructor(
- private db: AngularFireDatabase,
- private afAuth: AngularFireAuth
- ) {
- this.afAuth.authState.subscribe(auth => {
- if (auth !== undefined && auth !== null) {
- this.user = auth; // the user object is only going to be set if we are authenticated
- }
- this.getUser().subscribe(a => {
- this.userName = a.displayName;
- });
- });
- }
- getUser() { /*querying for the user id in the users node, which will contain the display name*/
- const userId = this.user.uid;
- const path = `/users/${userId}`;
- return this.db.object(path); /*this will return an observable that we can subscribe to*/
- }
Add Comment
Please, Sign In to add comment