Fix infinite calls to renew

This commit is contained in:
Kloon ImKloon 2023-09-03 14:54:05 +02:00
parent 13b87cd82a
commit 4d7ea79de9
No known key found for this signature in database
GPG Key ID: CCF1C86A995C5B6A
2 changed files with 26 additions and 34 deletions

View File

@ -148,16 +148,12 @@ const routes = [
}, },
]; ];
async function initialize() { async function initAuth() {
try {
if (loginPage) { if (loginPage) {
await validateLogin(); await validateLogin();
} else { } else {
await login("", "", ""); await login("", "", "");
} }
} catch (e) {
console.error(e);
}
if (recaptcha) { if (recaptcha) {
await new Promise((resolve) => { await new Promise((resolve) => {
@ -206,7 +202,11 @@ router.beforeResolve(async (to, from, next) => {
// this will only be null on first route // this will only be null on first route
if (from.name == null) { if (from.name == null) {
await initialize(); try {
await initAuth();
} catch (error) {
console.error(error);
}
} }
if (to.path.endsWith("/login") && authStore.isLoggedIn) { if (to.path.endsWith("/login") && authStore.isLoggedIn) {

View File

@ -1,8 +1,7 @@
import { useAuthStore } from "@/stores/auth"; import { useAuthStore } from "@/stores/auth";
import router from "@/router"; import router from "@/router";
import jwt_decode from "jwt-decode"; import jwt_decode from "jwt-decode";
import { fetchURL } from "@/api/utils"; import { baseURL } from "./constants";
import { baseURL } from "@/utils/constants";
export function parseToken(token) { export function parseToken(token) {
// falsy or malformed jwt will throw InvalidTokenError // falsy or malformed jwt will throw InvalidTokenError
@ -22,25 +21,22 @@ export async function validateLogin() {
if (localStorage.getItem("jwt")) { if (localStorage.getItem("jwt")) {
await renew(localStorage.getItem("jwt")); await renew(localStorage.getItem("jwt"));
} }
} catch (_) { } catch (error) {
console.warn("Invalid JWT token in storage"); // eslint-disable-line console.warn("Invalid JWT token in storage"); // eslint-disable-line
throw error;
} }
} }
export async function login(username, password, recaptcha) { export async function login(username, password, recaptcha) {
const data = { username, password, recaptcha }; const data = { username, password, recaptcha };
const res = await fetchURL( const res = await fetch(`${baseURL}/api/login`, {
`/api/login`,
{
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify(data), body: JSON.stringify(data),
}, });
false
);
const body = await res.text(); const body = await res.text();
@ -52,7 +48,7 @@ export async function login(username, password, recaptcha) {
} }
export async function renew(jwt) { export async function renew(jwt) {
const res = await fetchURL(`/api/renew`, { const res = await fetch(`${baseURL}/api/renew`, {
method: "POST", method: "POST",
headers: { headers: {
"X-Auth": jwt, "X-Auth": jwt,
@ -71,17 +67,13 @@ export async function renew(jwt) {
export async function signup(username, password) { export async function signup(username, password) {
const data = { username, password }; const data = { username, password };
const res = await fetchURL( const res = await fetch(`${baseURL}/api/signup`, {
`/api/signup`,
{
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify(data), body: JSON.stringify(data),
}, });
false
);
if (res.status !== 200) { if (res.status !== 200) {
throw new Error(res.status); throw new Error(res.status);