| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- "use strict";
- import {fetchRegular} from "./utils.js";
- 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");
- /**
- * Add button event listeners, for login and register
- */
- window.addEventListener("load", function () {
- submitButtonLogin.addEventListener("click", async function () {
- await login();
- })
- submitButtonRegister.addEventListener("click", async function () {
- await register();
- })
- });
- /**
- * Function to handle sending authentication requests.
- *
- * @param {string} type - The type of request, either 'login' or 'register'.
- */
- async function sendAuthRequest(type) {
- const response = await fetchRegular(
- "/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("/html/appointments.html");
- } else {
- if (response_json["error"]) {
- errorMessageParagraph.innerText = response_json["error"];
- } else {
- errorMessageParagraph.innerText = response_json["message"];
- }
- }
- }
- /**
- * Function to handle user login.
- */
- async function login() {
- await sendAuthRequest("login");
- }
- /**
- * Function to handle user registration.
- */
- async function register() {
- await sendAuthRequest("register");
- }
|