-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebase.js
More file actions
72 lines (56 loc) · 1.87 KB
/
firebase.js
File metadata and controls
72 lines (56 loc) · 1.87 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
import { initializeApp } from "firebase/app";
import {
getAuth,
signInWithPopup,
GoogleAuthProvider,
signOut,
onAuthStateChanged,
} from "firebase/auth";
import { getFirestore, doc, getDoc, setDoc } from "firebase/firestore";
import { getStorage } from "firebase/storage";
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_REACT_APP_API_KEY,
authDomain: process.env.NEXT_PUBLIC_REACT_APP_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_REACT_APP_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_REACT_APP_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_REACT_APP_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_REACT_APP_APP_ID,
measurementId: process.env.NEXT_PUBLIC_REACT_APP_MEASUREMENT_ID,
};
const firebaseApp = 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(firebaseApp);
export const storage = getStorage(firebaseApp);
export const createUserDocumentFromAuth = async (
userAuth,
additionalInformation = {}
) => {
if (!userAuth) return;
const userDocRef = doc(db, "users", userAuth.uid);
const userSnapshot = await getDoc(userDocRef);
if (!userSnapshot.exists()) {
const { displayName, email } = userAuth;
const createdAt = new Date();
const typeUser = "user";
try {
await setDoc(userDocRef, {
displayName,
email,
createdAt,
typeUser,
...additionalInformation,
});
} catch (error) {
console.log("Error creando el usuario", error.message);
}
}
return userDocRef;
};
export const signOutUser = async () => await signOut(auth);
export const onAuthStateChangedListener = (callback) =>
onAuthStateChanged(auth, callback);