diff --git a/frontend/src/router/index.js b/frontend/src/router/index.js index 060ceeba..31035b49 100644 --- a/frontend/src/router/index.js +++ b/frontend/src/router/index.js @@ -148,15 +148,11 @@ const routes = [ }, ]; -async function initialize() { - try { - if (loginPage) { - await validateLogin(); - } else { - await login("", "", ""); - } - } catch (e) { - console.error(e); +async function initAuth() { + if (loginPage) { + await validateLogin(); + } else { + await login("", "", ""); } if (recaptcha) { @@ -206,7 +202,11 @@ router.beforeResolve(async (to, from, next) => { // this will only be null on first route if (from.name == null) { - await initialize(); + try { + await initAuth(); + } catch (error) { + console.error(error); + } } if (to.path.endsWith("/login") && authStore.isLoggedIn) { diff --git a/frontend/src/utils/auth.js b/frontend/src/utils/auth.js index ecfa5b6b..8e5fbcba 100644 --- a/frontend/src/utils/auth.js +++ b/frontend/src/utils/auth.js @@ -1,8 +1,7 @@ import { useAuthStore } from "@/stores/auth"; import router from "@/router"; import jwt_decode from "jwt-decode"; -import { fetchURL } from "@/api/utils"; -import { baseURL } from "@/utils/constants"; +import { baseURL } from "./constants"; export function parseToken(token) { // falsy or malformed jwt will throw InvalidTokenError @@ -22,25 +21,22 @@ export async function validateLogin() { if (localStorage.getItem("jwt")) { await renew(localStorage.getItem("jwt")); } - } catch (_) { + } catch (error) { console.warn("Invalid JWT token in storage"); // eslint-disable-line + throw error; } } export async function login(username, password, recaptcha) { const data = { username, password, recaptcha }; - const res = await fetchURL( - `/api/login`, - { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(data), + const res = await fetch(`${baseURL}/api/login`, { + method: "POST", + headers: { + "Content-Type": "application/json", }, - false - ); + body: JSON.stringify(data), + }); const body = await res.text(); @@ -52,7 +48,7 @@ export async function login(username, password, recaptcha) { } export async function renew(jwt) { - const res = await fetchURL(`/api/renew`, { + const res = await fetch(`${baseURL}/api/renew`, { method: "POST", headers: { "X-Auth": jwt, @@ -71,17 +67,13 @@ export async function renew(jwt) { export async function signup(username, password) { const data = { username, password }; - const res = await fetchURL( - `/api/signup`, - { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(data), + const res = await fetch(`${baseURL}/api/signup`, { + method: "POST", + headers: { + "Content-Type": "application/json", }, - false - ); + body: JSON.stringify(data), + }); if (res.status !== 200) { throw new Error(res.status);