mails
This commit is contained in:
+20
-3
@@ -2,14 +2,30 @@ export function passwordKey(workspaceSlug) { return `rustpad:workspace:${workspa
|
||||
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.
|
||||
const NICKNAME_COOKIE = "rustpad_nickname";
|
||||
// A session cookie is shared by tabs, but disappears when the browser session
|
||||
// ends. The nickname is not an authentication secret.
|
||||
function getCookie(name) {
|
||||
const prefix = `${name}=`;
|
||||
const item = document.cookie.split("; ").find(value => value.startsWith(prefix));
|
||||
if (!item) return "";
|
||||
try { return decodeURIComponent(item.slice(prefix.length)); } catch { return ""; }
|
||||
}
|
||||
function setNicknameCookie(value) {
|
||||
if (value) document.cookie = `${NICKNAME_COOKIE}=${encodeURIComponent(value)}; Path=/; SameSite=Lax`;
|
||||
else document.cookie = `${NICKNAME_COOKIE}=; Path=/; SameSite=Lax; Max-Age=0`;
|
||||
}
|
||||
localStorage.removeItem(NICKNAME_KEY);
|
||||
export function getNickname() { return sessionStorage.getItem(NICKNAME_KEY) || ""; }
|
||||
export function getNickname() {
|
||||
const nickname = sessionStorage.getItem(NICKNAME_KEY) || getCookie(NICKNAME_COOKIE);
|
||||
if (nickname) sessionStorage.setItem(NICKNAME_KEY, nickname);
|
||||
return nickname;
|
||||
}
|
||||
export function setNickname(value) {
|
||||
const nickname = value.trim();
|
||||
if (nickname) sessionStorage.setItem(NICKNAME_KEY, nickname);
|
||||
else sessionStorage.removeItem(NICKNAME_KEY);
|
||||
setNicknameCookie(nickname);
|
||||
}
|
||||
|
||||
const AUTH_TOKEN_KEY = "rustpad:auth-token";
|
||||
@@ -23,6 +39,7 @@ export function clearAuthSession() {
|
||||
sessionStorage.removeItem(AUTH_TOKEN_KEY);
|
||||
localStorage.removeItem(NICKNAME_KEY);
|
||||
sessionStorage.removeItem(NICKNAME_KEY);
|
||||
setNicknameCookie("");
|
||||
}
|
||||
export async function resolveIdentity(api, nickname) {
|
||||
const result = await api("/api/auth/identity", { method: "POST", body: JSON.stringify({ nickname, session_token: getAuthToken() || null }) });
|
||||
|
||||
Reference in New Issue
Block a user