Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
64 lines (57 loc) · 2.03 KB
/
Copy pathscript.js
File metadata and controls
64 lines (57 loc) · 2.03 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
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.12.0/firebase-app.js";
import {
getAuth,
signInWithEmailAndPassword,
createUserWithEmailAndPassword,
onAuthStateChanged
} from "https://www.gstatic.com/firebasejs/10.12.0/firebase-auth.js";
// Firebase config
const firebaseConfig = {
apiKey: "AIzaSyD03vMpBpU5TOYwLy-hpImkku-MvwzANLw",
authDomain: "srecsocial-98c6f.firebaseapp.com",
projectId: "srecsocial-98c6f",
storageBucket: "srecsocial-98c6f.appspot.com",
messagingSenderId: "92453012487",
appId: "1:92453012487:web:1ee9723799a929c64356a2",
measurementId: "G-NYDLMEV2TE"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
// Auto redirect if already logged in
onAuthStateChanged(auth, (user) => {
if (user) {
window.location.href = "./home.html";
}
});
// Handle Login
window.login = async function () {
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
const msg = document.getElementById("message");
try {
const userCredential = await signInWithEmailAndPassword(auth, email, password);
msg.textContent = `✅ Logged in as ${userCredential.user.email}`;
msg.className = "success";
setTimeout(() => window.location.href = "home.html", 1000);
} catch (err) {
msg.textContent = err.message.includes("auth/invalid-credential")
? "❌ Invalid credentials. Please check your email/password."
: "❌ " + err.message;
msg.className = "error";
}
};
// Handle Signup
window.signup = async function () {
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
const msg = document.getElementById("message");
try {
const userCredential = await createUserWithEmailAndPassword(auth, email, password);
msg.textContent = `✅ Account created: ${userCredential.user.email}`;
msg.className = "success";
} catch (err) {
msg.textContent = "❌ " + err.message;
msg.className = "error";
}
};