Files
bitlogin/packages/widget/src/globalSession.ts
T
thePR0M3TH3ANandClaude Fable 5 a7cf55ea84 window.bitlogin.activeMethod()/activeSession(): session state for every method
Hosts had no method-agnostic session check: isActiveSigner() answers who
backs window.nostr, which is deliberately false during a NIP-07 session
(the extension IS the signer and rightly owns the slot) even though the
user signed in through BitLogin. Sites using it as a login check
mis-read extension sessions.

A page-level registry (globalSession.ts, public data only -- method,
pubkey, npub) is written at the single dispatchLogin point and cleared
on logout and DOM removal, owner-scoped so one element's teardown can't
clear another's session. window.bitlogin gains activeMethod() and
activeSession(); the integration guide and llms.txt now spell out the
two-questions distinction and lead with the one-login-surface pattern.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-04 10:58:14 -04:00

34 lines
1.2 KiB
TypeScript

/** Page-level record of the widget session, backing window.bitlogin.activeMethod()
* and activeSession() (docs/login-methods.md §LM3).
*
* window.nostr ownership (isActiveSigner) and session state are DIFFERENT
* questions with different answers: during a NIP-07 session the extension owns
* the slot -- correctly -- yet the user very much signed in through BitLogin.
* Hosts that used isActiveSigner() as a session check got `false` there. This
* registry answers the session question directly, for every method.
*
* Public data only (method, public key, npub); never key material. Owner-scoped
* so one element's teardown cannot clear a session another element established.
*/
import type { SignerMethod } from "./signers/types.js";
export interface ActiveSessionInfo {
method: SignerMethod;
publicKey: string;
npub: string;
}
let current: { owner: object; info: ActiveSessionInfo } | null = null;
export function setActiveSession(owner: object, info: ActiveSessionInfo): void {
current = { owner, info };
}
export function clearActiveSession(owner: object): void {
if (current?.owner === owner) current = null;
}
export function getActiveSession(): ActiveSessionInfo | null {
return current ? { ...current.info } : null;
}