-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfirebase.js
More file actions
116 lines (104 loc) · 3.34 KB
/
firebase.js
File metadata and controls
116 lines (104 loc) · 3.34 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
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
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import {
GithubAuthProvider,
GoogleAuthProvider,
createUserWithEmailAndPassword,
getAuth,
signInWithEmailAndPassword,
signInWithPopup,
signOut,
} from "firebase/auth";
import { get, getDatabase, ref, set } from "firebase/database";
const firebaseConfig = {
apiKey: "AIzaSyA4EGC6IBIPSgu7VNJRV2fEqf-HtOcvodI",
authDomain: "utillsjs2024.firebaseapp.com",
databaseURL: "https://utillsjs2024-default-rtdb.firebaseio.com",
projectId: "utillsjs2024",
storageBucket: "utillsjs2024.appspot.com",
messagingSenderId: "191989815985",
appId: "1:191989815985:web:15fceda0963ddd2abfa090",
measurementId: "G-W7WJLHMKRW",
};
// Initialize Firebase
export const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
export const db = getDatabase(app);
export const signUp = async (userData) => {
const userDetails = await createUserWithEmailAndPassword(
auth,
userData.email,
userData.password
)
.then(function (userCredential) {
// Save the user data to Firebase database using their unique ID as the key
saveUserData(userCredential);
})
.catch(function (error) {
console.error("Error registering user: ", error);
});
// console.log(userDetails);
return userDetails;
};
export const loginWithEmail = async (userData) => {
const userDetails = await signInWithEmailAndPassword(
auth,
userData.email,
userData.password
);
return userDetails;
};
export const loginWithSocial = async (socialDetails) => {
const googleAuthProvider = new GoogleAuthProvider();
const githubAuthProvider = new GithubAuthProvider();
switch (socialDetails) {
case "google": // Corrected to string 'gmail'
try {
const userDetails = await signInWithPopup(auth, googleAuthProvider); // Pass userData and googleAuthProvider to signInWithPopup
// Save the user data to Firebase database using their unique ID as the key
saveUserData(userDetails);
return userDetails;
} catch (error) {
console.log("there was an error", error);
}
break;
case "github": // Added case for GitHub authentication
try {
const userDetails = signInWithPopup(auth, githubAuthProvider); // Pass userData and googleAuthProvider to signInWithPopup
// Save the user data to Firebase database using their unique ID as the key
saveUserData(userDetails);
return userDetails;
} catch (error) {
console.log("there was an error", error);
}
break;
default:
break;
}
};
export const logout = () => {
signOut(auth);
};
// Function to save user data to Firebase database
async function saveUserData(userData) {
const { displayName, phoneNumber, photoURL, emailVerified, email, uid } =
userData.user;
// Get a reference to the location where user data will be stored
const userRef = ref(db, "usersData/" + uid);
// Check if user data already exists
const snapshot = await get(userRef);
// If user data doesn't exist, set the user data
if (!snapshot.exists()) {
set(userRef, {
displayName,
phoneNumber,
photoURL,
emailVerified,
email,
uid,
});
console.log("User data saved successfully!");
} else {
console.log("User data already exists.");
}
}