filebrowser/frontend/src/utils/auth.js

104 lines
2.4 KiB
JavaScript
Raw Normal View History

2021-03-21 11:51:58 +00:00
import store from "@/store";
import router from "@/router";
import { Base64 } from "js-base64";
import { baseURL } from "@/utils/constants";
2018-02-01 12:17:04 +00:00
2021-03-21 11:51:58 +00:00
export function parseToken(token) {
const parts = token.split(".");
if (parts.length !== 3) {
2021-03-21 11:51:58 +00:00
throw new Error("token malformed");
2018-02-01 12:17:04 +00:00
}
2021-03-21 11:51:58 +00:00
const data = JSON.parse(Base64.decode(parts[1]));
2021-04-19 12:49:40 +00:00
document.cookie = `auth=${token}; path=/`;
2021-03-21 11:51:58 +00:00
localStorage.setItem("jwt", token);
store.commit("setJWT", token);
2023-08-17 21:46:49 +00:00
store.commit("setSession", generateRandomCode(8));
2021-03-21 11:51:58 +00:00
store.commit("setUser", data.user);
2018-02-01 12:17:04 +00:00
}
2021-03-21 11:51:58 +00:00
export async function validateLogin() {
try {
2021-03-21 11:51:58 +00:00
if (localStorage.getItem("jwt")) {
await renew(localStorage.getItem("jwt"));
2018-02-01 12:17:04 +00:00
}
} catch (_) {
console.warn('Invalid JWT token in storage') // eslint-disable-line
}
}
2021-03-21 11:51:58 +00:00
export async function login(username, password, recaptcha) {
const data = { username, password, recaptcha };
const res = await fetch(`${baseURL}/api/login`, {
2021-03-21 11:51:58 +00:00
method: "POST",
headers: {
2021-03-21 11:51:58 +00:00
"Content-Type": "application/json",
},
2021-03-21 11:51:58 +00:00
body: JSON.stringify(data),
});
const body = await res.text();
if (res.status === 200) {
2021-03-21 11:51:58 +00:00
parseToken(body);
} else {
2021-03-21 11:51:58 +00:00
throw new Error(body);
}
2018-02-01 12:17:04 +00:00
}
2021-03-21 11:51:58 +00:00
export async function renew(jwt) {
const res = await fetch(`${baseURL}/api/renew`, {
2021-03-21 11:51:58 +00:00
method: "POST",
headers: {
2021-03-21 11:51:58 +00:00
"X-Auth": jwt,
},
});
2021-03-21 11:51:58 +00:00
const body = await res.text();
if (res.status === 200) {
2023-08-17 21:46:49 +00:00
store.commit("setSession", generateRandomCode(8));
2021-03-21 11:51:58 +00:00
parseToken(body);
} else {
2021-03-21 11:51:58 +00:00
throw new Error(body);
}
2018-02-01 12:17:04 +00:00
}
2023-08-17 21:46:49 +00:00
function generateRandomCode(length) {
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let code = '';
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * charset.length);
code += charset[randomIndex];
}
return code;
}
2021-03-21 11:51:58 +00:00
export async function signup(username, password) {
const data = { username, password };
const res = await fetch(`${baseURL}/api/signup`, {
2021-03-21 11:51:58 +00:00
method: "POST",
headers: {
2021-03-21 11:51:58 +00:00
"Content-Type": "application/json",
},
2021-03-21 11:51:58 +00:00
body: JSON.stringify(data),
});
if (res.status !== 200) {
2021-03-21 11:51:58 +00:00
throw new Error(res.status);
}
2018-02-01 12:17:04 +00:00
}
2021-03-21 11:51:58 +00:00
export function logout() {
2021-04-19 12:49:40 +00:00
document.cookie = "auth=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/";
2021-03-21 11:51:58 +00:00
store.commit("setJWT", "");
store.commit("setUser", null);
localStorage.setItem("jwt", null);
router.push({ path: "/login" });
2018-02-01 12:17:04 +00:00
}