From 02bfbabcfc030593de271e91cc216f321b86a3f3 Mon Sep 17 00:00:00 2001 From: Reimar Date: Thu, 27 Mar 2025 09:08:34 +0100 Subject: [PATCH] Set auth token when logging in --- frontend/scripts/login.js | 8 +++++++- frontend/shared/utils.js | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/frontend/scripts/login.js b/frontend/scripts/login.js index 162a418..74a8f33 100644 --- a/frontend/scripts/login.js +++ b/frontend/scripts/login.js @@ -10,10 +10,16 @@ document.getElementById("loginForm").addEventListener("submit", function(event) login(emailOrUsername, password) .then(response => { + document.cookie = `auth-token=${response.token}`; + + localStorage.setItem("user", { + id: response.id, + username: response.userName, + }); + location.href = "/home"; }) .catch(error => { - console.log(error); document.getElementById("form-error").innerText = error; document.getElementById("form-error").style.display = "block"; }); diff --git a/frontend/shared/utils.js b/frontend/shared/utils.js index d3c1964..30badae 100644 --- a/frontend/shared/utils.js +++ b/frontend/shared/utils.js @@ -1,7 +1,7 @@ import { address } from "./constants.js"; export async function request(method, path, body = null) { - const token = document.cookie.match(/\bauth-token=(\S+)/); + const token = document.cookie.match(/\bauth-token=([^;\s]+)/); return new Promise((resolve, reject) => { fetch(address + path, {