Skip to content

Commit 9057b8d

Browse files
authored
Update app.js
1 parent 2350f2b commit 9057b8d

File tree

1 file changed

+61
-37
lines changed

1 file changed

+61
-37
lines changed

src/app.js

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,85 @@ const noteTitle = document.getElementById("noteTitle");
33
const noteContent = document.getElementById("noteContent");
44
const addNoteBtn = document.getElementById("addNote");
55
const notesList = document.getElementById("notesList");
6-
const loginBtn = document.getElementById("loginBtn");
6+
const openAuthModalBtn = document.getElementById("openAuthModal");
77
const logoutBtn = document.getElementById("logoutBtn");
88

9-
// --- Combined Login/Signup --- //
10-
loginBtn.addEventListener("click", async () => {
11-
const email = prompt("Enter your email:");
12-
const password = prompt("Enter your password:");
9+
const authModal = document.getElementById("authModal");
10+
const closeModal = document.querySelector(".close");
11+
const modalTitle = document.getElementById("modalTitle");
12+
const authForm = document.getElementById("authForm");
13+
const authSubmitBtn = document.getElementById("authSubmitBtn");
14+
const toggleLink = document.getElementById("toggleLink");
1315

14-
if (!email || !password) return alert("Email and password required!");
16+
let isLogin = true; // toggle state
17+
18+
// --- Open modal ---
19+
openAuthModalBtn.addEventListener("click", () => authModal.style.display = "block");
20+
closeModal.addEventListener("click", () => authModal.style.display = "none");
21+
window.addEventListener("click", e => { if(e.target == authModal) authModal.style.display = "none"; });
22+
23+
// --- Toggle login/signup ---
24+
toggleLink.addEventListener("click", e => {
25+
e.preventDefault();
26+
isLogin = !isLogin;
27+
if(isLogin){
28+
modalTitle.textContent = "Login";
29+
authSubmitBtn.textContent = "Login";
30+
toggleLink.textContent = "Sign Up";
31+
} else {
32+
modalTitle.textContent = "Sign Up";
33+
authSubmitBtn.textContent = "Sign Up";
34+
toggleLink.textContent = "Login";
35+
}
36+
});
37+
38+
// --- Auth submit ---
39+
authForm.addEventListener("submit", async e => {
40+
e.preventDefault();
41+
const email = document.getElementById("emailInput").value.trim();
42+
const password = document.getElementById("passwordInput").value.trim();
1543

1644
try {
17-
// Try to sign in
18-
await auth.signInWithEmailAndPassword(email, password);
19-
alert("Logged in successfully!");
20-
} catch (err) {
21-
// If user doesn't exist, create account
22-
if (err.code === "auth/user-not-found") {
23-
try {
24-
await auth.createUserWithEmailAndPassword(email, password);
25-
alert("Account created and logged in!");
26-
} catch (signupErr) {
27-
alert("Signup failed: " + signupErr.message);
28-
}
29-
} else if (err.code === "auth/wrong-password") {
30-
alert("Wrong password!");
45+
if(isLogin){
46+
await auth.signInWithEmailAndPassword(email,password);
47+
alert("Logged in successfully!");
3148
} else {
32-
alert("Login error: " + err.message);
49+
await auth.createUserWithEmailAndPassword(email,password);
50+
alert("Account created and logged in!");
3351
}
52+
authModal.style.display = "none";
53+
authForm.reset();
54+
} catch(err){
55+
alert(err.message);
3456
}
3557
});
3658

37-
// Logout
59+
// --- Logout ---
3860
logoutBtn.addEventListener("click", () => auth.signOut());
3961

40-
// --- Listen for auth changes --- //
62+
// --- Listen auth state ---
4163
auth.onAuthStateChanged(user => {
42-
if (user) {
64+
if(user){
4365
addNoteBtn.disabled = false;
66+
logoutBtn.style.display = "inline-block";
67+
openAuthModalBtn.style.display = "none";
4468
listenNotes(user.uid);
4569
} else {
46-
notesList.innerHTML = "<p>Please log in to see your notes.</p>";
4770
addNoteBtn.disabled = true;
71+
logoutBtn.style.display = "none";
72+
openAuthModalBtn.style.display = "inline-block";
73+
notesList.innerHTML = "<p>Please log in to see your notes.</p>";
4874
}
4975
});
5076

51-
// --- Add note --- //
77+
// --- Notes functionality ---
5278
addNoteBtn.addEventListener("click", () => {
5379
const user = auth.currentUser;
54-
if (!user) return alert("Log in first!");
80+
if(!user) return alert("Log in first!");
5581

5682
const title = noteTitle.value.trim();
5783
const content = noteContent.value.trim();
58-
if (!title || !content) return alert("Both fields required!");
84+
if(!title || !content) return alert("Both fields required!");
5985

6086
db.collection("notes").add({
6187
title,
@@ -68,14 +94,13 @@ addNoteBtn.addEventListener("click", () => {
6894
});
6995
});
7096

71-
// --- Listen to notes in real-time --- //
72-
function listenNotes(uid) {
97+
function listenNotes(uid){
7398
db.collection("notes")
74-
.where("userId", "==", uid)
75-
.orderBy("timestamp", "desc")
76-
.onSnapshot(snapshot => {
99+
.where("userId","==",uid)
100+
.orderBy("timestamp","desc")
101+
.onSnapshot(snapshot=>{
77102
notesList.innerHTML = "";
78-
snapshot.forEach(doc => {
103+
snapshot.forEach(doc=>{
79104
const note = doc.data();
80105
const div = document.createElement("div");
81106
div.className = "note-card";
@@ -89,9 +114,8 @@ function listenNotes(uid) {
89114
});
90115
}
91116

92-
// --- Delete note --- //
93-
function deleteNote(id) {
117+
function deleteNote(id){
94118
const user = auth.currentUser;
95-
if (!user) return alert("Log in first!");
119+
if(!user) return alert("Log in first!");
96120
db.collection("notes").doc(id).delete();
97121
}

0 commit comments

Comments
 (0)