create user firestore boilerplate and logout function

This commit is contained in:
talksik
2022-05-28 11:45:47 -05:00
parent af32ad2565
commit 7c05f27199
4 changed files with 85 additions and 3 deletions
+5
View File
@@ -2,9 +2,14 @@
import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
import { getFirestore } from 'firebase/firestore';
// eslint-disable-next-line @typescript-eslint/no-var-requires
import devConfig from './config/firebase.dev.config.json';
export const firebaseApp = initializeApp(devConfig);
export const firebaseAuth = getAuth(firebaseApp);
// Initialize Cloud Firestore and get a reference to the service
export const firestore = getFirestore(firebaseApp);
@@ -0,0 +1,66 @@
//TODO: move all of this to core or common and separate out files
import {
setDoc,
getFirestore,
doc,
collection,
QueryDocumentSnapshot,
Timestamp,
FieldValue,
} from 'firebase/firestore';
import { User as FirebaseUser } from 'firebase/auth';
/**
* UTILS
*/
const converter = <T>() => ({
toFirestore: (data: T) => data,
fromFirestore: (snap: QueryDocumentSnapshot) => snap.data() as T,
});
const dataPoint = <T>(collectionPath: string) =>
doc(getFirestore(), collectionPath).withConverter(converter<T>());
const db = {
users: dataPoint<User[]>('users'),
user: (userId: string) => dataPoint<User>(`users/${userId}`),
};
export class User {
lastUpdatedDate?: Timestamp;
constructor(
public uid: string,
public providerId: string,
public email: string,
public displayName?: string,
public photoUrl?: string,
public phoneNumber?: string,
public createdDate: Timestamp = Timestamp.now(),
) {}
}
// enum COLLECTION {
// users = 'users',
// }
export const createUser = async (user: FirebaseUser) => {
try {
await setDoc(
db.user(user.uid),
new User(
user.uid,
user.providerId,
user.email,
user.displayName,
user.photoURL,
user.phoneNumber,
),
{ merge: true },
);
} catch (e) {
console.error('Error creating user: ', e);
}
};