From e39ec813a1db4696f6121ccd15aebdec8af8fc0d Mon Sep 17 00:00:00 2001 From: Jeff Huleatt <3759507+jhuleatt@users.noreply.github.com> Date: Sat, 11 May 2024 16:04:08 -0400 Subject: [PATCH] subscribe to user in chat service --- .../src/app/services/chat.service.ts | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/angularfire-start/src/app/services/chat.service.ts b/angularfire-start/src/app/services/chat.service.ts index b359f9614..f7ded70db 100644 --- a/angularfire-start/src/app/services/chat.service.ts +++ b/angularfire-start/src/app/services/chat.service.ts @@ -9,7 +9,7 @@ import { getAuth, User, } from '@angular/fire/auth'; -import { map, switchMap, firstValueFrom, filter, Observable } from 'rxjs'; +import { map, switchMap, firstValueFrom, filter, Observable, Subscription } from 'rxjs'; import { doc, docData, @@ -29,6 +29,7 @@ import { limit, onSnapshot, DocumentData, + FieldValue, } from '@angular/fire/firestore'; import { Storage, @@ -39,6 +40,16 @@ import { import { getToken, Messaging, onMessage } from '@angular/fire/messaging'; import { Router } from '@angular/router'; +type ChatMessage = { + name: string | null, + profilePicUrl: string | null, + timestamp: FieldValue, + uid: string | null, + text?: string, + imageUrl?: string +}; + + @Injectable({ providedIn: 'root', }) @@ -51,8 +62,16 @@ export class ChatService { private provider = new GoogleAuthProvider(); LOADING_IMAGE_URL = 'https://www.google.com/images/spin-32.gif?a'; - // Observable user + // observable that is updated when the auth state changes user$ = user(this.auth); + currentUser: User | null = this.auth.currentUser; + userSubscription: Subscription; + + constructor() { + this.userSubscription = this.user$.subscribe((aUser: User | null) => { + this.currentUser = aUser; + }); + } // Login Friendly Chat. login() {}