-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.js
67 lines (60 loc) · 2.34 KB
/
auth.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
import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.6.1/firebase-app.js';
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, signOut, onAuthStateChanged } from 'https://www.gstatic.com/firebasejs/9.6.1/firebase-auth.js';
const firebaseConfig = {
apiKey: "AIzaSyD8kL3cEoh2t_QuiHqk0x21BNV9knd01rU",
authDomain: "code-editor-efd72.firebaseapp.com",
projectId: "code-editor-efd72",
storageBucket: "code-editor-efd72.firebasestorage.app",
messagingSenderId: "28165650101",
appId: "1:28165650101:web:16c852e94780ac5f11b0fc",
measurementId: "G-82HBJ4TR9S"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
console.log("Firebase initialized:", app); // Debugging statement
// Register user
export async function registerUser(email, password) {
try {
const userCredential = await createUserWithEmailAndPassword(auth, email, password);
console.log("Signup successful:", userCredential);
alert("Signup successful!");
window.location.href = "dashboard.html"; // Redirect after signup
} catch (error) {
console.error("Signup error:", error.message);
alert(error.message); // Show error message
}
}
// Login user
export async function loginUser(email, password) {
try {
const userCredential = await signInWithEmailAndPassword(auth, email, password);
console.log("Login successful:", userCredential); // Debugging statement
alert("Login successful!");
window.location.href = "dashboard.html"; // Redirect after login
} catch (error) {
console.error("Login error:", error.message);
alert(error.message); // Show error message
}
}
// Logout user
export async function logoutUser() {
try {
await signOut(auth);
alert("Logged out!");
window.location.href = "index.html"; // Redirect to home page
} catch (error) {
console.error("Logout error:", error.message);
alert(error.message); // Show error message
}
}
// Check user state
export function checkUserState(callback) {
onAuthStateChanged(auth, (user) => {
if (user) {
callback(user); // Pass user data to callback
} else {
window.location.href = "login.html"; // Redirect to login if not logged in
}
});
}