Skip to content

Commit dc98912

Browse files
committed
Refactor : fetch items
1 parent b938d64 commit dc98912

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ <h2 class="form__title">Sign Up</h2>
2222
<div class="container__form container--signin">
2323
<form action="#" class="form" id="form2">
2424
<h2 class="form__title">Sign In</h2>
25-
<input type="email" placeholder="Email" class="input" />
25+
<input type="text" placeholder="Email" class="input" />
2626
<input type="password" placeholder="Password" class="input" />
2727
<a href="#" class="link">Forgot your password?</a>
2828
<button class="btn">Sign In</button>

js/script.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,40 @@ const fistForm = document.querySelector("#form1");
44
const secondForm = document.querySelector("#form2");
55
const container = document.querySelector(".container");
66

7-
87
signInBtn.addEventListener("click", () => {
9-
container.classList.remove("right-panel-active");
8+
container.classList.remove("right-panel-active");
109
});
1110

1211
signUpBtn.addEventListener("click", () => {
13-
container.classList.add("right-panel-active");
12+
container.classList.add("right-panel-active");
1413
});
1514

1615
fistForm.addEventListener("submit", (e) => e.preventDefault());
17-
secondForm.addEventListener("submit", (e) => e.preventDefault());
16+
secondForm.addEventListener("submit", (e) => handleLogin(e));
17+
18+
async function handleLogin(e) {
19+
e.preventDefault();
20+
21+
const email = secondForm.querySelector("input[type='text']").value;
22+
const password = secondForm.querySelector("input[type='password']").value;
23+
24+
try {
25+
const response = await fetch("https://fakestoreapi.com/auth/login", {
26+
method: "POST",
27+
headers: {
28+
"Content-Type": "application/json",
29+
},
30+
body: JSON.stringify({ email, password }),
31+
});
32+
if (!response.ok) {
33+
throw new Error("Login failed! Please check your credentials.");
34+
}
35+
const data = await response.json();
36+
localStorage.setItem("token", data.token);
37+
alert("Login successful!");
38+
39+
} catch (error) {
40+
console.error(error);
41+
alert(error.message);
42+
}
43+
}

0 commit comments

Comments
 (0)