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,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) {

View File

@ -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);