Fix infinite calls to renew
This commit is contained in:
parent
13b87cd82a
commit
4d7ea79de9
@ -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) {
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user