diff options
Diffstat (limited to 'src/features/profile/services/profileService.ts')
| -rw-r--r-- | src/features/profile/services/profileService.ts | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/features/profile/services/profileService.ts b/src/features/profile/services/profileService.ts new file mode 100644 index 0000000..566c3fa --- /dev/null +++ b/src/features/profile/services/profileService.ts @@ -0,0 +1,45 @@ +// src/features/profile/services/profileService.ts + +import { doc, getDoc, updateDoc } from 'firebase/firestore'; +import { updateProfile } from 'firebase/auth'; +import { db, auth } from '@/shared/lib/firebase/config'; +import { UserProfile } from '@/features/profile/types'; + +export const profileService = { + async getMyProfile(uid: string): Promise<UserProfile | null> { + const snapshot = await getDoc(doc(db, 'users', uid)); + + if (!snapshot.exists()) { + return null; + } + + const data = snapshot.data(); + + return { + uid, + email: data.email ?? '', + displayName: data.displayName ?? '', + photoURL: data.photoURL ?? '', + }; + }, + + async updateMyProfile(params: { + uid: string; + displayName: string; + photoURL?: string; + }): Promise<void> { + const { uid, displayName, photoURL } = params; + + await updateDoc(doc(db, 'users', uid), { + displayName, + ...(photoURL !== undefined ? { photoURL } : {}), + }); + + if (auth.currentUser) { + await updateProfile(auth.currentUser, { + displayName, + ...(photoURL !== undefined ? { photoURL } : {}), + }); + } + }, +}; |