-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebaseConfig.js
58 lines (49 loc) · 1.51 KB
/
firebaseConfig.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
import { getFirestore, doc, setDoc, getDoc } from 'firebase/firestore';
import firebase from 'firebase/compat/app';
import 'firebase/compat/firestore';
import 'firebase/compat/auth';
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: 'spaceoutcat.firebaseapp.com',
projectId: 'spaceoutcat',
storageBucket: 'spaceoutcat.appspot.com',
messagingSenderId: '1083711553888',
appId: '1:1083711553888:web:f930275a45765fc5931e05',
measurementId: 'G-E8VK78VYFL',
};
firebase.initializeApp(firebaseConfig);
const db = getFirestore();
const provider = new firebase.auth.GoogleAuthProvider();
provider.setCustomParameters({ prompt: 'select_account' });
export const auth = firebase.auth();
export const firestore = firebase.firestore();
// Function to sign in with Google
export const signInWithGoogle = async (router) => {
try {
const res = await auth.signInWithPopup(provider);
const user = res.user;
const userId = user.uid;
const userRef = doc(db, 'users', userId);
const docSnapshot = await getDoc(userRef);
if (!docSnapshot.exists()) {
await setDoc(userRef, {
uid: userId,
name: user.displayName,
authProvider: 'google',
email: user.email,
notification: false,
});
}
router.push('/profile');
} catch (err) {
throw new Error(err.message);
}
};
// Function to sign out
export const signOut = async () => {
try {
await auth.signOut();
} catch (err) {
throw new Error(err.message);
}
};