Fix infinite calls to renew
This commit is contained in:
parent
13b87cd82a
commit
4d7ea79de9
@ -148,15 +148,11 @@ 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) {
|
||||||
@ -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) {
|
||||||
|
|||||||
@ -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",
|
||||||
{
|
headers: {
|
||||||
method: "POST",
|
"Content-Type": "application/json",
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify(data),
|
|
||||||
},
|
},
|
||||||
false
|
body: JSON.stringify(data),
|
||||||
);
|
});
|
||||||
|
|
||||||
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",
|
||||||
{
|
headers: {
|
||||||
method: "POST",
|
"Content-Type": "application/json",
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify(data),
|
|
||||||
},
|
},
|
||||||
false
|
body: JSON.stringify(data),
|
||||||
);
|
});
|
||||||
|
|
||||||
if (res.status !== 200) {
|
if (res.status !== 200) {
|
||||||
throw new Error(res.status);
|
throw new Error(res.status);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user