Multi-Account Management
The library supports multiple accounts (called sessions) through different signers.
Adding a Session
Each time you use sessions.login NDK will create a new session if that signer isn't already an active session.
ts
// Login first account (automatically active)
const signer1 = new NDKPrivateKeySigner(nsec1);
const pubkey1 = await sessions.login(signer1);
// Login second account
const signer2 = new NDKPrivateKeySigner(nsec2);
const pubkey2 = await sessions.login(signer2, { setActive: false });
console.log("Accounts:", sessions.getSessions().size);Switch Between Accounts
Sessions can be listed with getSessions() which will return a Map<Hexpubkey, NDKSession>. Switching sessions is as simple as passing in the pubkey:
ts
// Switch to different account
sessions.switchTo(pubkey2);
console.log("Active:", sessions.activePubkey);
// Switch back
sessions.switchTo(pubkey1);