nomadro/src/modules/social/lib/socialSession.ts
Eric 3e60ba1a1b Ship social places/join UX, room-archive ops, and rich Markdown ntfy alerts.
Unify city+cafe into one places page, gate join with on-page modal, and enrich ntfy with icons, tables, and action buttons across portal/API/admin.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-05 12:21:01 +08:00

110 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client";
/** Lightweight email session for social join / test-phase auth. */
import { resetGuestId } from "./api";
import { clearLivenessState } from "./livenessGate";
import { removeJson } from "./storage";
const EMAIL_KEY = "userEmail";
const NAME_KEY = "userName";
const CHANGE = "nomadro-social-storage";
/** Profile / discovery keys bound to one email — must not leak across accounts. */
const ACCOUNT_JSON_KEYS = [
"join-profile",
"roster",
"nick",
"feedback-draft",
"feedback-delivery",
] as const;
export type SocialSession = {
email: string;
name: string;
};
function bump(): void {
if (typeof window === "undefined") return;
window.dispatchEvent(new Event(CHANGE));
}
/** Drop join/pay/face/profile that belong to the previous email. */
export function clearAccountBoundLocalState(): void {
try {
localStorage.removeItem("social-join-paid");
localStorage.removeItem("social-join-profile-done");
localStorage.removeItem("social-join-pending-profile");
localStorage.removeItem("paidType");
sessionStorage.removeItem("social-join-paid");
sessionStorage.removeItem("social-join-pending-profile");
sessionStorage.removeItem("social-join-profile-done");
} catch {
/* ignore */
}
for (const key of ACCOUNT_JSON_KEYS) {
removeJson(key);
}
clearLivenessState();
// 必须换 guest,否则后台按 guestId 拉花名册会串上一位的头像/自拍
resetGuestId();
}
export function getSocialSession(): SocialSession | null {
if (typeof window === "undefined") return null;
try {
const email = (localStorage.getItem(EMAIL_KEY) || "").trim().toLowerCase();
if (!email || !email.includes("@")) return null;
const name =
(localStorage.getItem(NAME_KEY) || "").trim() ||
email.split("@")[0] ||
"游民";
return { email, name };
} catch {
return null;
}
}
export function isSocialLoggedIn(): boolean {
return Boolean(getSocialSession()?.email);
}
/** Login = persist email (used as account id for join pay). */
export function loginWithEmail(email: string, name?: string): SocialSession {
const value = email.trim().toLowerCase();
if (!value.includes("@")) throw new Error("invalid email");
const prev = (localStorage.getItem(EMAIL_KEY) || "").trim().toLowerCase();
// 换邮箱 / 首登:清空上一位资料头像核验,禁止串号
if (prev !== value) {
clearAccountBoundLocalState();
}
const nick =
(name || "").trim() || value.split("@")[0] || "游民";
localStorage.setItem(EMAIL_KEY, value);
localStorage.setItem(NAME_KEY, nick);
bump();
return { email: value, name: nick };
}
/** Logout clears email session + join local flags (guest id kept). */
export function logoutSocialSession(): void {
try {
localStorage.removeItem(EMAIL_KEY);
localStorage.removeItem(NAME_KEY);
} catch {
/* ignore */
}
clearAccountBoundLocalState();
bump();
}
export function subscribeSocialSession(cb: () => void): () => void {
if (typeof window === "undefined") return () => undefined;
window.addEventListener(CHANGE, cb);
window.addEventListener("storage", cb);
return () => {
window.removeEventListener(CHANGE, cb);
window.removeEventListener("storage", cb);
};
}