-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
61 lines (53 loc) · 2.31 KB
/
script.js
File metadata and controls
61 lines (53 loc) · 2.31 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
<script>
// Firebase configuration (Replace with your actual Firebase project credentials)
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_PROJECT_ID.appspot.com",
messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
appId: "YOUR_APP_ID"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
// Register new user
document.getElementById("registerForm").addEventListener("submit", function(event) {
event.preventDefault(); // Prevent form from refreshing the page
const email = document.getElementById("regEmail").value;
const password = document.getElementById("regPassword").value;
auth.createUserWithEmailAndPassword(email, password)
.then(userCredential => {
alert("Registration Successful!");
console.log("User registered:", userCredential.user);
})
.catch(error => {
alert("Error: " + error.message);
console.error("Registration Error:", error);
});
});
// Login user
document.getElementById("loginForm").addEventListener("submit", function(event) {
event.preventDefault();
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
auth.signInWithEmailAndPassword(email, password)
.then(userCredential => {
alert("Login Successful!");
console.log("User logged in:", userCredential.user);
document.getElementById("authMessage").innerText = "Welcome, " + userCredential.user.email;
})
.catch(error => {
alert("Error: " + error.message);
console.error("Login Error:", error);
});
});
// Check authentication state (Optional)
auth.onAuthStateChanged(user => {
if (user) {
document.getElementById("authMessage").innerText = "Logged in as " + user.email;
} else {
document.getElementById("authMessage").innerText = "Not logged in";
}
});
</script>