bigblocks
Version:
Complete Bitcoin UI component library - authentication, social, wallet, market, inscriptions, and blockchain interactions for React
123 lines (122 loc) • 4 kB
JavaScript
import { useCallback, useEffect, useState } from "react";
import { useBitcoinAuthContext } from "../components/providers/BitcoinAuthProvider.js";
export function useBitcoinAuth() {
const context = useBitcoinAuthContext();
const [walletExtension, setWalletExtension] = useState(null);
// Extract commonly used values
const { user, isAuthenticated, isLoading, error, currentStep, mode, hasLocalBackup, hasWalletCapabilities, linkedProviders,
// Actions
signIn, signUp, signOut, generateBackup, importBackup, validatePassword, linkOAuth, createProfile, switchProfile, setStep, setMode, reset,
// Config
config,
// Storage check methods
hasUnencryptedBackupInSession, hasEncryptedBackupStored, isSessionBackupImported, } = context;
// Helper methods with better ergonomics
const isReady = !isLoading;
const hasError = !!error;
const hasLinkedProviders = linkedProviders.length > 0;
// Profile helpers
const profiles = user?.profiles || [];
const activeProfile = profiles.find((p) => p.id === user?.activeProfileId);
const profileCount = profiles.length;
// Step helpers
const isSigningIn = mode === "signin";
const isSigningUp = mode === "signup";
const isLinking = mode === "link";
// Wrapped actions with better error handling
const signInWithPassword = useCallback(async (password) => {
try {
await signIn(password);
return { success: true };
}
catch (error) {
return { success: false, error: error };
}
}, [signIn]);
const signUpWithPassword = useCallback(async (password) => {
try {
await signUp(password);
return { success: true };
}
catch (error) {
return { success: false, error: error };
}
}, [signUp]);
const importBackupFile = useCallback(async (file) => {
try {
await importBackup(file);
return { success: true };
}
catch (error) {
return { success: false, error: error };
}
}, [importBackup]);
// Load wallet extension when wallet capabilities are available
useEffect(() => {
const loadWalletExtension = async () => {
if (hasWalletCapabilities && isAuthenticated) {
try {
const walletExt = await context.getWalletExtension();
setWalletExtension(walletExt);
}
catch (error) {
console.warn("Failed to load wallet extension:", error);
setWalletExtension(null);
}
}
else {
setWalletExtension(null);
}
};
loadWalletExtension();
}, [hasWalletCapabilities, isAuthenticated, context]);
return {
// State
user,
isAuthenticated,
isLoading,
isReady,
error,
hasError,
currentStep,
mode,
hasLocalBackup,
hasWalletCapabilities,
walletExtension,
linkedProviders,
hasLinkedProviders,
// Profile state
profiles,
activeProfile,
profileCount,
// Mode helpers
isSigningIn,
isSigningUp,
isLinking,
// Core actions
signIn: signInWithPassword,
signUp: signUpWithPassword,
signOut,
// Backup actions
generateBackup,
importBackup: importBackupFile,
validatePassword,
// OAuth actions
linkOAuth,
// Profile actions
createProfile,
switchProfile,
// Flow control
setStep,
setMode,
reset,
// Raw actions (for advanced use)
actions: context,
// Config
config,
// Storage check methods
hasUnencryptedBackupInSession,
hasEncryptedBackupStored,
isSessionBackupImported,
};
}