-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
123 lines (104 loc) · 3.74 KB
/
script.js
File metadata and controls
123 lines (104 loc) · 3.74 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
114
115
116
117
118
119
120
121
122
123
// Password toggle functionality
document.addEventListener("DOMContentLoaded", function () {
const eyeButton = document.querySelector(".eye-icon");
const passwordInput = document.querySelector(
'.password-field input[type="password"]',
);
const eyeIcon = eyeButton.querySelector("svg");
eyeButton.addEventListener("click", function () {
if (passwordInput.type === "password") {
passwordInput.type = "text";
eyeIcon.innerHTML = `
<path d="M17.94 17.94A10.07 10.07 0 0 1 12 20c-7 0-11-8-11-8a18.45 18.45 0 0 1 5.06-5.94M9.9 4.24A9.12 9.12 0 0 1 12 4c7 0 11 8 11 8a18.5 18.5 0 0 1-2.16 3.19m-6.72-1.07a3 3 0 1 1-4.24-4.24"></path>
<line x1="1" y1="1" x2="23" y2="23"></line>
`;
} else {
passwordInput.type = "password";
eyeIcon.innerHTML = `
<path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path>
<circle cx="12" cy="12" r="3"></circle>
`;
}
});
// Form validation
const form = document.querySelector(".signup-form");
const createAccountBtn = document.querySelector(".create-account-btn");
form.addEventListener("submit", async function (e) {
e.preventDefault();
//get from ele
const firstName = document.querySelector('input[placeholder="First name"]');
const lastName = document.querySelector('input[placeholder="Last name"]');
const email = document.querySelector('input[placeholder="Email"]');
const password = passwordInput;
const checkbox = document.querySelector(".checkbox");
let isValid = true;
if (!firstName.value.trim()) {
firstName.style.borderColor = "#ef4444";
isValid = false;
} else {
firstName.style.borderColor = "#3a3a4a";
}
if (!lastName.value.trim()) {
lastName.style.borderColor = "#ef4444";
isValid = false;
} else {
lastName.style.borderColor = "#3a3a4a";
}
if (!email.value.trim() || !email.value.includes("@")) {
email.style.borderColor = "#ef4444";
isValid = false;
} else {
email.style.borderColor = "#3a3a4a";
}
if (!password.value.trim() || password.value.length < 6) {
password.style.borderColor = "#ef4444";
isValid = false;
} else {
password.style.borderColor = "#3a3a4a";
}
if (!checkbox.checked) {
checkbox.parentElement.style.color = "#ef4444";
isValid = false;
} else {
checkbox.parentElement.style.color = "#ffffff";
}
if (!isValid) return;
try {
createAccountBtn.innerHTML = "Creating account...";
createAccountBtn.disabled = true;
const response = await fetch("http://localhost:5000/api/auth/signup", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
firstName: firstName.value.trim(),
lastName: lastName.value.trim(),
email: email.value.trim(),
password: password.value.trim(),
}),
});
const data = await response.json();
if (!response.ok) {
throw new Error(data.message || "Signup Failed");
}
if (data.token) {
localStorage.setItem("token", data.token);
localStorage.setItem("userData", JSON.stringify({
firstName: data.user.firstName,
lastName: data.user.lastName,
email: data.user.email
}));
}
alert(data.message || "Account created successfully!");
// Redirect to dashboard after successful signup
setTimeout(() => {
window.location.href = "dashboard.html";
}, 500);
form.reset();
} catch (error) {
alert(error.message || "Something Went wrong");
} finally {
createAccountBtn.innerHTML = "Create account";
createAccountBtn.disabled = false;
}
});
});