SkateSpots/src/hooks/useCurrentUser.ts
shakespeare.diy 52bf618b51 Fix amber login type: use x-amber instead of amber
NLoginOther requires type x-${string} to be a valid NLoginType.
Using bare 'amber' meant pubkey was never properly stored/rehydrated
from localStorage — it was always undefined/empty.

Using NLogin('x-amber', pubkey, ...) correctly stores pubkey through
the NLoginBase interface and survives localStorage serialization.

Co-authored-by: shakespeare.diy <assistant@shakespeare.diy>
2026-07-09 14:57:20 +07:00

66 lines
2.3 KiB
TypeScript

import { type NLoginType, NUser, useNostrLogin } from '@nostrify/react/login';
import { useNostr } from '@nostrify/react';
import { useCallback, useMemo } from 'react';
import { useAuthor } from './useAuthor.ts';
import { AmberSigner } from '@/lib/amber';
import { CapacitorAmberSigner } from '@/lib/capacitorAmberSigner';
import type { AmberLoginType } from './useLoginActions';
/** Extended login type that includes Amber */
type ExtendedLoginType = NLoginType | AmberLoginType;
export function useCurrentUser() {
const { nostr } = useNostr();
const { logins } = useNostrLogin();
const loginToUser = useCallback((login: ExtendedLoginType): NUser => {
switch (login.type) {
case 'nsec': // Nostr login with secret key
return NUser.fromNsecLogin(login);
case 'bunker': // Nostr login with NIP-46 "bunker://" URI
return NUser.fromBunkerLogin(login, nostr);
case 'extension': // Nostr login with NIP-07 browser extension
return NUser.fromExtensionLogin(login);
case 'x-amber': { // Nostr login with Amber (NIP-55)
const amberLogin = login as AmberLoginType;
// On Capacitor (ZapStore APK) use the native plugin signer so all
// signing operations go through Amber via native intents.
// On web/PWA fall back to the URL-redirect based AmberSigner.
const isCapacitor = !!(window as unknown as { Capacitor?: { isNativePlatform?: () => boolean } }).Capacitor?.isNativePlatform?.();
const signer = isCapacitor
? new CapacitorAmberSigner(amberLogin.pubkey)
: new AmberSigner(amberLogin.pubkey);
return new NUser({ pubkey: amberLogin.pubkey, signer });
}
// Other login types can be defined here
default:
throw new Error(`Unsupported login type: ${(login as ExtendedLoginType).type}`);
}
}, [nostr]);
const users = useMemo(() => {
const users: NUser[] = [];
for (const login of logins) {
try {
const user = loginToUser(login);
users.push(user);
} catch (error) {
console.warn('Skipped invalid login', login.id, error);
}
}
return users;
}, [logins, loginToUser]);
const user = users[0] as NUser | undefined;
const author = useAuthor(user?.pubkey);
return {
user,
users,
isLoadingProfile: author.isLoading,
...author.data,
};
}