|
| 1 | +// const logInForm = document.querySelector(".log-in-form"); |
| 2 | +// logInForm.addEventListener("submit", async (e) => { |
| 3 | +// e.preventDefault(); |
| 4 | +// const formData = new FormData(logInForm); |
| 5 | +// const email = formData.get("email"); |
| 6 | +// const password = formData.get("password"); |
| 7 | +// const body = { email, password }; |
| 8 | +// try { |
| 9 | +// const res = await fetch(`${process.env.BACKEND_URL}/users/token`, { |
| 10 | +// method: "POST", |
| 11 | +// body: JSON.stringify(body), |
| 12 | +// headers: { |
| 13 | +// "Content-Type": "application/json", |
| 14 | +// }, |
| 15 | +// }); |
| 16 | +// if (!res.ok) { |
| 17 | +// throw res; |
| 18 | +// } |
| 19 | +// const { |
| 20 | +// token, |
| 21 | +// user: { id }, |
| 22 | +// } = await res.json(); |
| 23 | + |
| 24 | +// localStorage.setItem("TAPPDIN_ACCESS_TOKEN", token); |
| 25 | +// localStorage.setItem("TAPPDIN_CURRENT_USER_ID", id); |
| 26 | + |
| 27 | +// window.location.href = "/"; |
| 28 | +// } catch (err) { |
| 29 | +// if (err.status >= 400 && err.status < 600) { |
| 30 | +// const errorJSON = await err.json(); |
| 31 | +// const errorsContainer = document.querySelector(".errors-container"); |
| 32 | +// let errorsHtml = [ |
| 33 | +// ` |
| 34 | +// <div class="alert alert-danger"> |
| 35 | +// Something went wrong. Please try again. |
| 36 | +// </div> |
| 37 | +// `, |
| 38 | +// ]; |
| 39 | +// const { errors } = errorJSON; |
| 40 | +// if (errors && Array.isArray(errors)) { |
| 41 | +// errorsHtml = errors.map( |
| 42 | +// (message) => ` |
| 43 | +// <div class="alert alert-danger"> |
| 44 | +// ${message} |
| 45 | +// </div> |
| 46 | +// ` |
| 47 | +// ); |
| 48 | +// } |
| 49 | +// errorsContainer.innerHTML = errorsHtml.join(""); |
| 50 | +// } else { |
| 51 | +// alert( |
| 52 | +// "Something went wrong. Please check your internet connection and try again!" |
| 53 | +// ); |
| 54 | +// } |
| 55 | +// } |
| 56 | +// }); |
0 commit comments