|
|
@@ -0,0 +1,59 @@
|
|
|
+"use strict";
|
|
|
+
|
|
|
+const emailInput = document.getElementById("login-email");
|
|
|
+const passwordInput = document.getElementById("login-password");
|
|
|
+const errorMessageParagraph = document.getElementById("login-error");
|
|
|
+const submitButtonLogin = document.getElementById("btn-login");
|
|
|
+const submitButtonRegister = document.getElementById("btn-register");
|
|
|
+
|
|
|
+window.onload = function () {
|
|
|
+ submitButtonLogin.addEventListener("click", async function () {
|
|
|
+ await login();
|
|
|
+ })
|
|
|
+
|
|
|
+ submitButtonRegister.addEventListener("click", async function () {
|
|
|
+ await register();
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+async function sendAuthRequest(type) {
|
|
|
+ console.log(emailInput.value);
|
|
|
+ console.log(passwordInput.value);
|
|
|
+ const response = await fetch(
|
|
|
+ "/api/auth/" + type,
|
|
|
+ {
|
|
|
+ method: "POST",
|
|
|
+ headers: {
|
|
|
+ "Content-Type": "application/json", // Set the content type to JSON
|
|
|
+ },
|
|
|
+ body: JSON.stringify(
|
|
|
+ {
|
|
|
+ email: emailInput.value,
|
|
|
+ password: passwordInput.value,
|
|
|
+ }
|
|
|
+ ),
|
|
|
+ }
|
|
|
+ )
|
|
|
+ const response_json = await response.json();
|
|
|
+
|
|
|
+ if (response.status === 200) {
|
|
|
+ localStorage.setItem("token", response_json["token"]);
|
|
|
+ window.location.replace("/");
|
|
|
+ } else {
|
|
|
+ if (response_json["error"]) {
|
|
|
+ errorMessageParagraph.innerText = response_json["error"];
|
|
|
+ } else {
|
|
|
+ errorMessageParagraph.innerText = response_json["message"];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+async function login() {
|
|
|
+ await sendAuthRequest("login");
|
|
|
+}
|
|
|
+
|
|
|
+async function register() {
|
|
|
+ await sendAuthRequest("register");
|
|
|
+}
|