-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebase.js
More file actions
75 lines (59 loc) · 2.22 KB
/
firebase.js
File metadata and controls
75 lines (59 loc) · 2.22 KB
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
import { initializeApp } from "firebase/app";
import { getAuth, GoogleAuthProvider, signInWithPopup, createUserWithEmailAndPassword, signInWithEmailAndPassword } from 'firebase/auth'
import { getFirestore, doc, getDoc, setDoc } from 'firebase/firestore'
// 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
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "AIzaSyAsBErLZeP41qx90tUYuh_YiaIQ5P75kIc",
authDomain: "sit-313task-7-1p.firebaseapp.com",
projectId: "sit-313task-7-1p",
storageBucket: "sit-313task-7-1p.appspot.com",
messagingSenderId: "446281397367",
appId: "1:446281397367:web:ba703f76d26231381f0272",
measurementId: "G-ZEGRTPJD4Y"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const provider = new GoogleAuthProvider();
provider.setCustomParameters({
prompt: "select_account"
});
export const auth = getAuth();
export const signInWithGooglePopup = () => signInWithPopup(auth, provider)
export const db = getFirestore();
export const createuserdocfromAuth = async (userAuth, additionalInformation = {}) => {
if (!userAuth.email) return;
const userDocRef = doc(db, 'users', userAuth.uid);
console.log(userDocRef)
const userSnapShots = await getDoc(userDocRef);
console.log(userSnapShots)
console.log(userSnapShots.exists())
if (!userSnapShots.exists()) {
const { displayName, email } = userAuth
const createdAt = new Date();
try {
await setDoc(userDocRef, {
displayName,
email,
createdAt,
...additionalInformation
})
}
catch (error) {
console.log('error in creating', error.message)
}
}
return userDocRef;
}
export async function createAuthUserWithEmailAndPassword(email, password) {
if (!email || !password)
return
return await createUserWithEmailAndPassword(auth, email, password)
}
export async function signinAuthUserWithEmailAndPassword(email, password) {
if (!email || !password)
return
return await signInWithEmailAndPassword(auth, email, password)
}