31 lines
1.8 KiB
JavaScript
31 lines
1.8 KiB
JavaScript
export function passwordKey(workspaceSlug) { return `rustpad:workspace:${workspaceSlug}:password`; }
|
|
export function getPassword(workspaceSlug) { return sessionStorage.getItem(passwordKey(workspaceSlug)) || ""; }
|
|
export function setPassword(workspaceSlug, password) { if (password) sessionStorage.setItem(passwordKey(workspaceSlug), password); else sessionStorage.removeItem(passwordKey(workspaceSlug)); }
|
|
const NICKNAME_KEY = "rustpad:nickname";
|
|
// Nicknames belong to the current browser session. Remove the old persistent
|
|
// value so deleting/logging out of a session cannot leave an identity behind.
|
|
localStorage.removeItem(NICKNAME_KEY);
|
|
export function getNickname() { return sessionStorage.getItem(NICKNAME_KEY) || ""; }
|
|
export function setNickname(value) {
|
|
const nickname = value.trim();
|
|
if (nickname) sessionStorage.setItem(NICKNAME_KEY, nickname);
|
|
else sessionStorage.removeItem(NICKNAME_KEY);
|
|
}
|
|
|
|
const AUTH_TOKEN_KEY = "rustpad:auth-token";
|
|
const legacyAuthToken = localStorage.getItem(AUTH_TOKEN_KEY);
|
|
if (legacyAuthToken && !sessionStorage.getItem(AUTH_TOKEN_KEY)) sessionStorage.setItem(AUTH_TOKEN_KEY, legacyAuthToken);
|
|
localStorage.removeItem(AUTH_TOKEN_KEY);
|
|
export function getAuthToken() { return sessionStorage.getItem(AUTH_TOKEN_KEY) || ""; }
|
|
export function setAuthSession(session) { sessionStorage.setItem(AUTH_TOKEN_KEY, session.token); setNickname(session.nickname); }
|
|
export function clearAuthSession() {
|
|
localStorage.removeItem(AUTH_TOKEN_KEY);
|
|
sessionStorage.removeItem(AUTH_TOKEN_KEY);
|
|
localStorage.removeItem(NICKNAME_KEY);
|
|
sessionStorage.removeItem(NICKNAME_KEY);
|
|
}
|
|
export async function resolveIdentity(api, nickname) {
|
|
const result = await api("/api/auth/identity", { method: "POST", body: JSON.stringify({ nickname, session_token: getAuthToken() || null }) });
|
|
setNickname(result.nickname); return result;
|
|
}
|