feat: add SSO workspace, named shares and Debian release tooling
This commit is contained in:
1 parent
833d908884
commit
87bc33e675
106 files changed
+4160
-5637
No files matched your search
+33
-133
@@ -1,141 +1,41 @@
|
||||
import { useAuthStore } from "@/stores/auth";
|
||||
import router from "@/router";
|
||||
import type { JwtPayload } from "jwt-decode";
|
||||
import { jwtDecode } from "jwt-decode";
|
||||
import { authMethod, baseURL, noAuth, logoutPage } from "./constants";
|
||||
import { StatusError } from "@/api/utils";
|
||||
import { setSafeTimeout } from "@/api/utils";
|
||||
|
||||
export function parseToken(token: string) {
|
||||
// falsy or malformed jwt will throw InvalidTokenError
|
||||
const data = jwtDecode<JwtPayload & { user: IUser }>(token);
|
||||
|
||||
document.cookie = `auth=${token}; Path=/; SameSite=Strict;`;
|
||||
|
||||
localStorage.setItem("jwt", token);
|
||||
|
||||
const authStore = useAuthStore();
|
||||
authStore.jwt = token;
|
||||
authStore.setUser(data.user);
|
||||
|
||||
// proxy auth with custom logout subject to unknown external timeout
|
||||
if (logoutPage !== "/login" && authMethod === "proxy") {
|
||||
console.warn("idle timeout disabled with proxy auth and custom logout");
|
||||
return;
|
||||
}
|
||||
|
||||
if (authStore.logoutTimer) {
|
||||
clearTimeout(authStore.logoutTimer);
|
||||
}
|
||||
|
||||
const expiresAt = new Date(data.exp! * 1000);
|
||||
const timeout = expiresAt.getTime() - Date.now();
|
||||
authStore.setLogoutTimer(
|
||||
setSafeTimeout(() => {
|
||||
logout("inactivity");
|
||||
}, timeout)
|
||||
import { baseURL } from "./constants";
|
||||
import { setLocale, detectLocale } from "@/i18n";
|
||||
export function startLogin(
|
||||
target = window.location.pathname + window.location.search
|
||||
) {
|
||||
window.location.assign(
|
||||
baseURL + "/auth/login?return=" + encodeURIComponent(target)
|
||||
);
|
||||
}
|
||||
|
||||
export async function validateLogin() {
|
||||
try {
|
||||
if (localStorage.getItem("jwt")) {
|
||||
await renew(<string>localStorage.getItem("jwt"));
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("Invalid JWT token in storage");
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function login(
|
||||
username: string,
|
||||
password: string,
|
||||
recaptcha: string
|
||||
) {
|
||||
const data = { username, password, recaptcha };
|
||||
|
||||
const res = await fetch(`${baseURL}/api/login`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
let pending: Promise<void> | null = null;
|
||||
export function validateLogin(): Promise<void> {
|
||||
if (pending) return pending;
|
||||
pending = (async () => {
|
||||
const res = await fetch(baseURL + "/api/session", {
|
||||
credentials: "same-origin",
|
||||
cache: "no-store",
|
||||
});
|
||||
if (res.ok) {
|
||||
const user = await res.json();
|
||||
useAuthStore().setUser(user);
|
||||
await setLocale(user.locale || detectLocale());
|
||||
} else if (res.status === 401) useAuthStore().clearUser();
|
||||
else throw new Error("SSO session is temporarily unavailable");
|
||||
})().finally(() => {
|
||||
pending = null;
|
||||
});
|
||||
|
||||
const body = await res.text();
|
||||
|
||||
if (res.status === 200) {
|
||||
parseToken(body);
|
||||
} else {
|
||||
throw new StatusError(
|
||||
body || `${res.status} ${res.statusText}`,
|
||||
res.status
|
||||
);
|
||||
}
|
||||
return pending;
|
||||
}
|
||||
|
||||
export async function renew(jwt: string) {
|
||||
const res = await fetch(`${baseURL}/api/renew`, {
|
||||
export async function logout() {
|
||||
const res = await fetch(baseURL + "/auth/logout", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"X-Auth": jwt,
|
||||
},
|
||||
headers: { "X-FB-Request": "1" },
|
||||
credentials: "same-origin",
|
||||
});
|
||||
|
||||
const body = await res.text();
|
||||
|
||||
if (res.status === 200) {
|
||||
parseToken(body);
|
||||
} else {
|
||||
throw new StatusError(
|
||||
body || `${res.status} ${res.statusText}`,
|
||||
res.status
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function signup(username: string, password: string) {
|
||||
const data = { username, password };
|
||||
|
||||
const res = await fetch(`${baseURL}/api/signup`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
if (res.status !== 200) {
|
||||
const body = await res.text();
|
||||
throw new StatusError(
|
||||
body || `${res.status} ${res.statusText}`,
|
||||
res.status
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export function logout(reason?: string) {
|
||||
document.cookie = "auth=; Max-Age=0; Path=/; SameSite=Strict;";
|
||||
|
||||
const authStore = useAuthStore();
|
||||
authStore.clearUser();
|
||||
|
||||
localStorage.setItem("jwt", "");
|
||||
if (noAuth) {
|
||||
window.location.reload();
|
||||
} else if (logoutPage !== "/login") {
|
||||
document.location.href = `${logoutPage}`;
|
||||
} else {
|
||||
if (typeof reason === "string" && reason.trim() !== "") {
|
||||
router.push({
|
||||
path: "/login",
|
||||
query: { "logout-reason": reason },
|
||||
});
|
||||
} else {
|
||||
router.push({
|
||||
path: "/login",
|
||||
});
|
||||
}
|
||||
}
|
||||
if (!res.ok) throw new Error("Sign out failed. Please try again.");
|
||||
useAuthStore().clearUser();
|
||||
localStorage.removeItem("jwt");
|
||||
document.cookie = "auth=; Max-Age=0; Path=/; SameSite=Strict";
|
||||
window.location.assign(baseURL + "/login?logged-out=1");
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import "ace-builds";
|
||||
import { themesByName } from "ace-builds/src-noconflict/ext-themelist";
|
||||
import { getTheme } from "./theme";
|
||||
|
||||
export const getEditorTheme = (themeName: string) => {
|
||||
const key = themeName.replace(/^ace\/theme\//, "");
|
||||
if (themesByName[key] !== undefined) return `ace/theme/${key}`;
|
||||
return getTheme() === "dark" ? "ace/theme/twilight" : "ace/theme/chrome";
|
||||
};
|
||||
+14
-17
@@ -1,18 +1,29 @@
|
||||
import { theme } from "./constants";
|
||||
import "ace-builds";
|
||||
import { themesByName } from "ace-builds/src-noconflict/ext-themelist";
|
||||
|
||||
export const getTheme = (): UserTheme => {
|
||||
try {
|
||||
const saved = localStorage.getItem("fb.theme");
|
||||
if (saved === "light" || saved === "dark") return saved;
|
||||
} catch {
|
||||
/* Storage can be disabled by the browser. */
|
||||
}
|
||||
return (document.documentElement.className as UserTheme) || theme;
|
||||
};
|
||||
|
||||
export const setTheme = (theme: UserTheme) => {
|
||||
export const setTheme = (theme: UserTheme, persist = true) => {
|
||||
const html = document.documentElement;
|
||||
if (!theme) {
|
||||
html.className = getMediaPreference();
|
||||
} else {
|
||||
html.className = theme;
|
||||
}
|
||||
if (persist) {
|
||||
try {
|
||||
localStorage.setItem("fb.theme", html.className);
|
||||
} catch {
|
||||
/* Theme still applies without persistence. */
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const toggleTheme = (): void => {
|
||||
@@ -34,17 +45,3 @@ export const getMediaPreference = (): UserTheme => {
|
||||
return "light";
|
||||
}
|
||||
};
|
||||
|
||||
export const getEditorTheme = (themeName: string) => {
|
||||
if (!themeName.startsWith("ace/theme/")) {
|
||||
themeName = `ace/theme/${themeName}`;
|
||||
}
|
||||
const themeKey = themeName.replace("ace/theme/", "");
|
||||
if (themesByName[themeKey] !== undefined) {
|
||||
return themeName;
|
||||
} else if (getTheme() === "dark") {
|
||||
return "ace/theme/twilight";
|
||||
} else {
|
||||
return "ace/theme/chrome";
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user