-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase-client.ts
92 lines (79 loc) · 2.64 KB
/
firebase-client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* eslint-disable @typescript-eslint/no-explicit-any */
import { FirebaseApp, getApps, initializeApp } from 'firebase/app';
import {
createUserWithEmailAndPassword,
getAuth,
sendEmailVerification as sendVerification,
sendPasswordResetEmail as sendReset,
signInWithEmailAndPassword,
signOut,
updateProfile,
User,
UserCredential,
} from 'firebase/auth';
import { getFirestore, setLogLevel } from 'firebase/firestore';
import { getFunctions } from 'firebase/functions';
import { getStorage } from 'firebase/storage';
import { AuthUser } from './auth-user';
export * from 'firebase/firestore';
const config = process.env.NEXT_PUBLIC_APP_ID
? {
apiKey: process.env.NEXT_PUBLIC_API_KEY,
authDomain: process.env.NEXT_PUBLIC_AUTH_DOMAIN,
databaseURL: process.env.NEXT_PUBLIC_DATABASE_URL,
projectId: process.env.NEXT_PUBLIC_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_APP_ID,
measurementId: process.env.NEXT_PUBLIC_MEASUREMENT_ID,
}
: // the firebase-config file is copied to the base of the project
// before each of the apps is launched so this should always be
// updated according to the app that is currently running.
// see setup in package.json start scripts.
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('../firebase-config').firebaseConfig;
// const USE_EMULATOR = true;
const apps = getApps();
const firebaseApp: FirebaseApp =
apps && apps.length > 0 ? apps[0] : initializeApp(config);
export const auth = getAuth(firebaseApp);
export const firestore = getFirestore(firebaseApp);
export const storage = getStorage(firebaseApp, config.storageBucket);
export const functions = getFunctions(firebaseApp);
setLogLevel('debug');
// auth
export async function registerFirebase(
name: string,
email: string,
password: string
): Promise<void | User> {
const credential: UserCredential = await createUserWithEmailAndPassword(
auth,
email,
password
);
if (!credential) {
return;
}
const { user } = credential;
if (!name) {
return user;
}
return await updateProfile(user, { displayName: name });
}
export function loginFirebase(
email: string,
password: string
): Promise<UserCredential> {
return signInWithEmailAndPassword(auth, email, password);
}
export function logoutFirebase() {
return signOut(auth);
}
export function sendPasswordResetEmail(email: string, options) {
return sendReset(auth, email, options);
}
export function sendEmailVerification(user: AuthUser) {
return sendVerification(user);
}