Files
mkstack/src/hooks/useLoginActions.ts
T
Alex Gleason 9d43fdbcc3 Use window.nostr.connect, getSigner and disconnect where the extension offers them
Din's and Tenna's NIP-07 providers add three optional methods, after NIP-46's
names. Logging in through an extension now asks connect() for an account
(skipping the Welcome back preview, since the extension shows the account),
each extension login signs through getSigner(pubkey) so switching accounts
in the extension doesn't change who the app signs as, and logging out calls
disconnect(pubkey). Each is feature-detected on its own; extensions without
them work as before. The onchain-bitcoin skill's NBrowserSignerBtc now
builds on the same signer.
2026-11-28 16:50:29 -06:00

95 lines
3.5 KiB
TypeScript

import { useNostr } from '@nostrify/react';
import {
NLogin,
type NLoginType,
type NostrConnectParams,
type NostrConnectStatus,
useNostrLogin,
} from '@nostrify/react/login';
import { useAppContext } from '@/hooks/useAppContext';
import { APP_RELAYS } from '@/lib/appRelays';
import { getNip07Provider, hasConnect } from '@/lib/nip07';
// NOTE: This file should not be edited except for adding new login methods.
export type { NostrConnectParams, NostrConnectStatus };
export { generateNostrConnectParams, generateNostrConnectURI } from '@nostrify/react/login';
export function useLoginActions() {
const { nostr } = useNostr();
const { logins, addLogin, setLogin, removeLogin } = useNostrLogin();
const { config } = useAppContext();
// Add a login and promote it to be the current user. Without the
// setLogin call the new login is appended to the end of the array,
// leaving the prior account as logins[0] — which is what
// useCurrentUser / useLoggedInAccounts treat as the active user.
// Promoting here makes "Add another account" actually switch.
const addAndActivate = (login: NLoginType) => {
addLogin(login);
setLogin(login.id);
};
return {
// Login with a Nostr secret key
nsec(nsec: string): void {
const login = NLogin.fromNsec(nsec);
addAndActivate(login);
},
// Login with a NIP-46 "bunker://" URI
async bunker(uri: string): Promise<void> {
const login = await NLogin.fromBunker(uri, nostr);
addAndActivate(login);
},
// Login with a NIP-07 browser extension. One that can give the site an
// account of the user's choosing (connect) is asked for one; the
// extension shows the account itself.
async extension(): Promise<void> {
const provider = getNip07Provider();
if (hasConnect(provider)) {
const signer = await provider.connect();
const answer: unknown = await signer.getPublicKey();
const pubkey = typeof answer === 'string' ? answer.toLowerCase() : '';
if (!/^[0-9a-f]{64}$/.test(pubkey)) {
throw new Error('The extension returned an invalid public key');
}
addAndActivate(new NLogin('extension', pubkey, null));
return;
}
const login = await NLogin.fromExtension();
addAndActivate(login);
},
// Login via nostrconnect:// (client-initiated NIP-46)
// The client displays a QR code and waits for the remote signer to connect.
//
// `onStatus` is forwarded from @nostrify/react so the UI can render
// live progress through the handshake phases — see NostrConnectStatus.
async nostrconnect(
params: NostrConnectParams,
signal?: AbortSignal,
onStatus?: (status: NostrConnectStatus) => void,
): Promise<void> {
const login = await NLogin.fromNostrConnect(params, nostr, { signal, onStatus });
addAndActivate(login);
},
// Get the relay URLs for NIP-46 nostrconnect communication
getRelayUrls(): string[] {
const relays = config.relayMetadata.relays
.filter((r) => r.write)
.map((r) => r.url);
// Fall back to the app default relays if the user has none configured,
// so the remote signer has multiple connection options during handshake.
return relays.length > 0
? relays
: APP_RELAYS.relays.filter((r) => r.write).map((r) => r.url);
},
// Log out the current user
async logout(): Promise<void> {
const login = logins[0];
if (login) {
removeLogin(login.id);
}
}
};
}