-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase.js
159 lines (147 loc) · 4.33 KB
/
firebase.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"use client";
// Import the functions you need from the SDKs you need
import { getApps, initializeApp } from "firebase/app";
import {
connectFirestoreEmulator,
doc,
setDoc,
query,
collection,
where,
getDocs,
initializeFirestore,
persistentLocalCache,
persistentMultipleTabManager,
} from "firebase/firestore";
import {
getAuth,
connectAuthEmulator,
GoogleAuthProvider,
signInWithPopup,
createUserWithEmailAndPassword,
signInWithEmailAndPassword,
updateProfile,
signOut,
} from "firebase/auth";
import { DATABASE_PATH } from "./lib/variables";
import { toast } from "sonner";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
};
// Initialize Firebase
const app =
getApps().length === 0 ? initializeApp(firebaseConfig) : getApps()[0];
export default app;
// const appCheck = initializeAppCheck(app, {
// provider: new ReCaptchaV3Provider(
// process.env.NEXT_PUBLIC_RECAPTCHA_PUBLIC_KEY
// ),
// });
export const auth = getAuth(app);
export const firestoreDb = initializeFirestore(app, {
localCache: persistentLocalCache({
tabManager: persistentMultipleTabManager(),
}),
});
if (globalThis?.location?.hostname === "localhost") {
connectAuthEmulator(auth, "http://localhost:4001");
connectFirestoreEmulator(firestoreDb, "localhost", 4002);
} else if (
globalThis?.location?.hostname === process.env.NEXT_PUBLIC_LOCAL_TARGET_HOST
) {
connectAuthEmulator(
auth,
`http://${process.env.NEXT_PUBLIC_LOCAL_TARGET_HOST}:4001`
);
connectFirestoreEmulator(
firestoreDb,
process.env.NEXT_PUBLIC_LOCAL_TARGET_HOST,
4002
);
}
export async function setDocumentToUsersCollection(
uid,
obj,
path = DATABASE_PATH.users
) {
try {
await setDoc(doc(firestoreDb, path, uid), obj, {
merge: true,
});
return;
} catch (error) {
console.warn(error);
return toast.error(
"there was an error on setDocumentToUsersCollection " + error
);
}
}
export async function createUserWithPassword(name, email, password) {
try {
let { user } = await createUserWithEmailAndPassword(
auth,
email,
password
);
await setDocumentToUsersCollection(user.uid, {
uid: user.uid,
name,
email,
photoURL: null,
});
await updateProfile(auth.currentUser, {
displayName: name,
photoURL,
});
return user;
} catch (e) {
console.warn(e);
toast.error("There was an error creating user: " + name);
await signOut(auth);
return e.code;
}
}
export async function loginWithPassword(email, password) {
try {
let { user } = await signInWithEmailAndPassword(auth, email, password);
return user;
} catch (e) {
console.warn("There was an error at loginWithPassword");
return e.code;
}
}
export async function signInWithGoogle() {
try {
let provider = new GoogleAuthProvider();
let { user } = await signInWithPopup(auth, provider);
const userExists = await getDocs(
query(
collection(firestoreDb, DATABASE_PATH.users),
where("uid", "==", user.uid)
)
);
if (userExists.size !== 0) {
return user;
}
await setDocumentToUsersCollection(user.uid, {
uid: user.uid,
name: user.displayName,
email: user.email,
photoURL: user.photoURL,
});
return user;
} catch (e) {
console.warn("There was an error at signInWithGoogle", e);
toast.error("There was an error at signInWithGoogle: ", e);
await signOut(auth);
return e.code;
}
}