-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
145 lines (121 loc) · 4 KB
/
script.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const myForm = document.querySelector("#my-form");
const nameInput = document.querySelector("#name");
const numberInput = document.querySelector("#number");
const emailInput = document.querySelector("#email");
const emailInputLogin = document.querySelector("#emailLogin");
const passwordInput = document.querySelector("#password");
const passwordInputLogin = document.querySelector("#passwordLogin");
const msg = document.querySelector("#msg");
function validateEmail(email) {
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}
myForm.addEventListener("submit", (e) => {
e.preventDefault();
const user = {
name: nameInput.value,
number: numberInput.value,
email: emailInput.value,
password: passwordInput.value,
msg: "",
};
if (
user.name === "" ||
user.number === "" ||
user.email === "" ||
user.password === ""
) {
msg.classList.add("error");
msg.innerHTML = "Please enter all fields.";
user.msg = "Please enter all fields.";
setTimeout(() => {
msg.classList.remove("error");
msg.innerHTML = "";
}, 3000);
} else if (!validateEmail(user.email)) {
msg.classList.add("error");
msg.innerHTML = "Email is invalid.";
setTimeout(() => {
msg.classList.remove("error");
msg.innerHTML = "";
}, 3000);
} else if (user.password.length < 6) {
msg.classList.add("error");
msg.innerHTML = "Password must contain at least 6 characters.";
setTimeout(() => {
msg.classList.remove("error");
msg.innerHTML = "";
}, 3000);
} else {
msg.classList.add("success");
msg.innerHTML = "Successfully created an account!";
user.msg = "Successfully created an account!";
const userJSON = JSON.stringify(user);
localStorage.setItem("user", userJSON);
const token = Math.random() * 100000000000000000;
localStorage.setItem("token", token);
console.log(userJSON);
setTimeout(() => {
msg.classList.remove("success");
msg.innerHTML = "";
window.location.href = "/login.html";
}, 3000);
myForm.removeEventListener("submit", () => {
console.log("Submit event listener removed");
});
}
});
myForm.addEventListener("submit", (e) => {
e.preventDefault();
const getUserItem = localStorage.getItem("user");
const getUser = JSON.parse(getUserItem);
const user = {
email: emailInputLogin.value,
password: passwordInputLogin.value,
msg: "",
};
if (user.email === "" || user.password === "") {
msg.classList.add("error");
msg.innerHTML = "Please fill up all fields.";
user.msg = "Please fill up all fields.";
setTimeout(() => {
msg.classList.remove("error");
msg.innerHTML = "";
}, 3000);
} else if (!getUser) {
msg.classList.add("error");
msg.innerHTML = "This user does not exist.";
user.msg = "This user does not exist.";
setTimeout(() => {
msg.classList.remove("error");
msg.innerHTML = "";
}, 3000);
} else if (user.email !== getUser.email) {
msg.classList.add("error");
msg.innerHTML = "This email does not exist.";
user.msg = "This email does not exist.";
setTimeout(() => {
msg.classList.remove("error");
msg.innerHTML = "";
}, 3000);
} else if (user.password !== getUser.password) {
msg.classList.add("error");
msg.innerHTML = "Password is incorrect.";
user.msg = "Password is incorrect.";
setTimeout(() => {
msg.classList.remove("error");
msg.innerHTML = "";
}, 3000);
} else {
msg.classList.add("success");
msg.innerHTML = "You are logged in!";
user.msg = "You are logged in!";
setTimeout(() => {
msg.classList.remove("success");
msg.innerHTML = "";
const getUserToken = localStorage.getItem("token");
sessionStorage.setItem("firstLogin", getUserToken);
window.location.href = "/dashboard.html";
}, 3000);
}
});