get user info from npubxcash

This commit is contained in:
lemonknowsall committed 2025-10-13 13:10:02 -07:00
1 parent 1701cce6b0
commit c8ad3b1a36
2 files changed
+176 -49

No files matched your search

+71 -4
View File
@@ -1,16 +1,30 @@
import { useCallback, useState } from 'react';
import { useNIP98 } from './useNIP98';
const NPUBCASH_API_BASE = 'https://npub.cash/api/v2';
const NPUBCASH_API_BASE = 'https://npubx.cash/api/v2';
export interface NpubCashUserInfo {
pubkey: string;
name?: string;
mint_url: string;
lock_quote: boolean;
}
export interface NpubCashApiError {
error: true;
message: string;
}
export interface NpubCashUserInfoResponse {
error: false;
data: {
user: NpubCashUserInfo;
};
}
/**
* Hook for interacting with npub.cash API
* Currently supports setting the user's preferred mint
* Currently supports setting the user's preferred mint and getting user info
*/
export function useNpubCash() {
const { createAuthHeader, isLoggedIn, pubkey } = useNIP98();
@@ -46,8 +60,13 @@ export function useNpubCash() {
});
if (!response.ok) {
const errorData = await response.json() as NpubCashApiError;
throw new Error(errorData.message || 'Failed to set mint');
// Try to parse JSON error, but handle HTML responses (404, etc.)
try {
const errorData = await response.json() as NpubCashApiError;
throw new Error(errorData.message || `Failed to set mint (${response.status})`);
} catch (parseError) {
throw new Error(`Failed to set mint: ${response.status} ${response.statusText}`);
}
}
console.log(`✅ Successfully set preferred mint on npub.cash to: ${mintUrl}`);
@@ -62,8 +81,56 @@ export function useNpubCash() {
}
}, [isLoggedIn, createAuthHeader]);
/**
* Get user info from npub.cash
*/
const getUserInfo = useCallback(async (): Promise<NpubCashUserInfo | null> => {
if (!isLoggedIn) {
console.error('❌ User must be logged in to get npub.cash user info');
return null;
}
try {
setIsLoading(true);
setError(null);
const url = `${NPUBCASH_API_BASE}/user/info`;
const authHeader = await createAuthHeader({ url, method: 'GET' });
const response = await fetch(url, {
method: 'GET',
headers: {
'Authorization': authHeader,
'Content-Type': 'application/json',
},
});
if (!response.ok) {
// Try to parse JSON error, but handle HTML responses (404, etc.)
try {
const errorData = await response.json() as NpubCashApiError;
throw new Error(errorData.message || `Failed to get user info (${response.status})`);
} catch (parseError) {
throw new Error(`Failed to get user info: ${response.status} ${response.statusText}`);
}
}
const data = await response.json() as NpubCashUserInfoResponse;
console.log('✅ Successfully retrieved npub.cash user info:', data.data.user);
return data.data.user;
} catch (err) {
const errorMessage = err instanceof Error ? err.message : 'Unknown error';
console.error('❌ Failed to get npub.cash user info:', errorMessage);
setError(errorMessage);
return null;
} finally {
setIsLoading(false);
}
}, [isLoggedIn, createAuthHeader]);
return {
setMint,
getUserInfo,
isLoading,
error,
isLoggedIn,