-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
54 lines (44 loc) · 1.52 KB
/
auth.js
File metadata and controls
54 lines (44 loc) · 1.52 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
// Firebase config (same as yours)
const firebaseConfig = {
apiKey: "AIzaSyBdcqSSWmjGnn3fsi8eDQTXtFDFUiYbtfU",
authDomain: "devtrackr-149be.firebaseapp.com",
projectId: "devtrackr-149be",
storageBucket: "devtrackr-149be.firebasestorage.app",
messagingSenderId: "936962359530",
appId: "1:936962359530:web:0a27728821a2a02ea91793",
measurementId: "G-VX3H0BDW39",
};
firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
const emailInput = document.getElementById("email");
const passwordInput = document.getElementById("password");
const btnSignup = document.getElementById("btn-signup");
const btnLogin = document.getElementById("btn-login");
btnSignup.onclick = () => {
const email = emailInput.value.trim();
const password = passwordInput.value.trim();
if (!email || !password) return alert("Enter email and password");
auth
.createUserWithEmailAndPassword(email, password)
.then(() => {
alert("Account created. You can login now.");
})
.catch((err) => alert(err.message));
};
btnLogin.onclick = () => {
const email = emailInput.value.trim();
const password = passwordInput.value.trim();
if (!email || !password) return alert("Enter email and password");
auth
.signInWithEmailAndPassword(email, password)
.then(() => {
window.location.href = "dashboard.html";
})
.catch((err) => alert(err.message));
};
// If already logged in, go directly to dashboard
auth.onAuthStateChanged((user) => {
if (user) {
window.location.href = "dashboard.html";
}
});