kloon15 8838a09cf5
refactor: migrate frontend tooling to vite 4 (#2645)
---------

Co-authored-by: Oleg Lobanov <oleg@lobanov.me>
2023-08-26 13:55:51 +02:00

101 lines
2.0 KiB
JavaScript

import store from "@/store";
import router from "@/router";
import { Base64 } from "js-base64";
import { fetchURL } from "@/api/utils";
export function parseToken(token) {
const parts = token.split(".");
if (parts.length !== 3) {
throw new Error("token malformed");
}
const data = JSON.parse(Base64.decode(parts[1]));
document.cookie = `auth=${token}; path=/`;
localStorage.setItem("jwt", token);
store.commit("setJWT", token);
store.commit("setUser", data.user);
}
export async function validateLogin() {
try {
if (localStorage.getItem("jwt")) {
await renew(localStorage.getItem("jwt"));
}
} catch (_) {
console.warn("Invalid JWT token in storage"); // eslint-disable-line
}
}
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),
},
false
);
const body = await res.text();
if (res.status === 200) {
parseToken(body);
} else {
throw new Error(body);
}
}
export async function renew(jwt) {
const res = await fetchURL(`/api/renew`, {
method: "POST",
headers: {
"X-Auth": jwt,
},
});
const body = await res.text();
if (res.status === 200) {
parseToken(body);
} else {
throw new Error(body);
}
}
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),
},
false
);
if (res.status !== 200) {
throw new Error(res.status);
}
}
export function logout() {
document.cookie = "auth=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/";
store.commit("setJWT", "");
store.commit("setUser", null);
localStorage.setItem("jwt", null);
router.push({ path: "/login" });
}