55 lines
1.7 KiB
TypeScript
55 lines
1.7 KiB
TypeScript
import { useNostr } from '@nostrify/react';
|
|
import {
|
|
NLogin,
|
|
type NLoginType,
|
|
useNostrLogin,
|
|
} from '@/lib/nostrLogin';
|
|
|
|
// NOTE: This file should not be edited except for adding new login methods.
|
|
|
|
// NLoginOther requires type to be `x-${string}` — use x-amber so it
|
|
// is a valid NLoginType and pubkey is preserved through serialization.
|
|
export type AmberLoginType = NLoginType & { type: 'x-amber'; pubkey: string };
|
|
|
|
export function useLoginActions() {
|
|
const { nostr } = useNostr();
|
|
const { logins, addLogin, setLogin, removeLogin } = useNostrLogin();
|
|
|
|
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
|
|
async extension(): Promise<void> {
|
|
const login = await NLogin.fromExtension();
|
|
addAndActivate(login);
|
|
},
|
|
// Login with Amber (NIP-55) — pubkey returned from Amber plugin or callback
|
|
// Uses NLogin with type x-amber so it satisfies NLoginOther (x-${string})
|
|
// and pubkey is correctly stored and rehydrated from localStorage.
|
|
amber(pubkey: string): void {
|
|
const login = new NLogin('x-amber', pubkey, { method: 'nip55' }) as unknown as NLoginType;
|
|
addLogin(login);
|
|
setLogin(login.id);
|
|
},
|
|
// Log out the current user
|
|
async logout(): Promise<void> {
|
|
const login = logins[0];
|
|
if (login) {
|
|
removeLogin(login.id);
|
|
}
|
|
},
|
|
};
|
|
}
|