-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
113 lines (100 loc) · 3.37 KB
/
script.js
File metadata and controls
113 lines (100 loc) · 3.37 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
// Import Firebase functions
import { initializeApp } from "https://www.gstatic.com/firebasejs/12.4.0/firebase-app.js";
import {
getAuth,
createUserWithEmailAndPassword,
signInWithEmailAndPassword,
signOut,
signInWithPopup,
GoogleAuthProvider,
sendPasswordResetEmail,
onAuthStateChanged
} from "https://www.gstatic.com/firebasejs/12.4.0/firebase-auth.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/12.4.0/firebase-analytics.js";
// Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyAPPSJkIk7uM4yuSwGtYbGJiMbj23q2eFA",
authDomain: "authentication-app-3f05c.firebaseapp.com",
projectId: "authentication-app-3f05c",
storageBucket: "authentication-app-3f05c.firebasestorage.app",
messagingSenderId: "37141128681",
appId: "1:37141128681:web:e7c9a8f8c5cb084643d778",
measurementId: "G-W8DMJBCYFW",
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const auth = getAuth(app);
const provider = new GoogleAuthProvider();
// Sign Up with Email/Password
document.getElementById("signup-btn")?.addEventListener("click", () => {
const email = document.getElementById("signup-email").value;
const password = document.getElementById("signup-password").value;
createUserWithEmailAndPassword(auth, email, password)
.then(() => {
alert("Sign Up Successful!");
window.location.href = "welcome.html";
})
.catch((error) => {
alert(error.message);
});
});
// Login with Email/Password
document.getElementById("login-btn")?.addEventListener("click", () => {
const email = document.getElementById("login-email").value;
const password = document.getElementById("login-password").value;
signInWithEmailAndPassword(auth, email, password)
.then(() => {
alert("Login Successful!");
window.location.href = "welcome.html";
})
.catch((error) => {
alert(error.message);
});
});
// Continue with Google
document.getElementById("google-btn")?.addEventListener("click", () => {
signInWithPopup(auth, provider)
.then(() => {
alert("Login Successful!");
window.location.href = "welcome.html";
})
.catch((error) => {
alert(error.message);
});
});
// Logout
document.getElementById("logout-btn")?.addEventListener("click", () => {
signOut(auth)
.then(() => {
alert("Logged Out Successfully!");
window.location.href = "index.html";
})
.catch((error) => {
alert(error.message);
});
});
// Reset Password
document.getElementById("reset-password-link")?.addEventListener("click", (e) => {
e.preventDefault();
const email = prompt("Please enter your email address:");
if (email) {
sendPasswordResetEmail(auth, email)
.then(() => {
alert("Password reset email sent! Check your inbox.");
})
.catch((error) => {
alert("Error: " + error.message);
});
} else {
alert("Please enter a valid email address.");
}
});
// Show User Email on Welcome Page
onAuthStateChanged(auth, (user) => {
if (user && window.location.pathname.includes("welcome.html")) {
document.getElementById("user-email").textContent = user.email;
} else if (!user && window.location.pathname.includes("welcome.html")) {
window.location.href = "index.html";
}
});