bigblocks
Version:
Complete Bitcoin UI component library - authentication, social, wallet, market, inscriptions, and blockchain interactions for React
15 lines • 2.87 kB
JSON
{
"name": "metalens-hooks-usemetalensauth",
"type": "registry:hook",
"dependencies": [],
"devDependencies": [],
"registryDependencies": [],
"files": [
{
"path": "components/metalens/hooks/useMetaLensAuth.ts",
"type": "registry:hook",
"content": "\"use client\";\n\nimport { useCallback } from \"react\";\nimport { useBitcoinAuth } from \"../../../hooks/useBitcoinAuth.js\";\nimport type { SignedComment, UseMetaLensAuthReturn } from \"../types/index.js\";\n\n/**\n * MetaLens authentication hook integrated with BigBlocks auth\n *\n * Based on patterns from:\n * - BigBlocks authentication system\n * - MetaLens signing requirements\n *\n * @example\n * ```tsx\n * const { isAuthenticated, signComment } = useMetaLensAuth();\n *\n * if (!isAuthenticated) {\n * await authenticate();\n * }\n *\n * const signed = await signComment('Hello MetaLens!');\n * ```\n */\nexport function useMetaLensAuth(): UseMetaLensAuthReturn {\n\tconst { user, signIn, signOut } = useBitcoinAuth();\n\n\tconst isAuthenticated = !!user;\n\n\tconst authenticate = useCallback(async () => {\n\t\tif (user) return; // Already authenticated\n\n\t\ttry {\n\t\t\tawait signIn(\"existing\");\n\t\t} catch (error) {\n\t\t\tconsole.error(\"Failed to authenticate:\", error);\n\t\t\tthrow new Error(\"Authentication failed\");\n\t\t}\n\t}, [user, signIn]);\n\n\tconst signComment = useCallback(\n\t\tasync (content: string): Promise<SignedComment> => {\n\t\t\tif (!user) {\n\t\t\t\tthrow new Error(\"User must be authenticated to sign comments\");\n\t\t\t}\n\n\t\t\t// Create timestamp for signing\n\t\t\tconst timestamp = new Date().toISOString();\n\n\t\t\t// Create message to sign (content + timestamp)\n\t\t\tconst _message = `${content}${timestamp}`;\n\n\t\t\t// For now, we'll return a placeholder since actual signing\n\t\t\t// depends on the Bitcoin auth implementation details\n\t\t\t// In production, this would use the user's signing key\n\n\t\t\tconst mapData = {\n\t\t\t\tapp: \"bigblocks-metalens\",\n\t\t\t\ttype: \"comment\",\n\t\t\t\ttimestamp,\n\t\t\t};\n\n\t\t\treturn {\n\t\t\t\tcontent,\n\t\t\t\tsignature: \"placeholder-signature\", // TODO: Implement actual signing\n\t\t\t\tpublicKey: user.address || \"\",\n\t\t\t\tmapData,\n\t\t\t};\n\t\t},\n\t\t[user],\n\t);\n\n\treturn {\n\t\tisAuthenticated,\n\t\tauthenticate,\n\t\tuser: user\n\t\t\t? {\n\t\t\t\t\taddress: user.address || \"\",\n\t\t\t\t\t// Note: These fields come from ProfileInfo, not AuthUser directly\n\t\t\t\t\tpaymail: user.profiles?.[0]?.paymail,\n\t\t\t\t\tbapId: user.idKey, // Use idKey for BAP ID\n\t\t\t\t\tavatar: user.profiles?.[0]?.image,\n\t\t\t\t\tname: user.profiles?.[0]?.alternateName || user.profiles?.[0]?.name,\n\t\t\t\t}\n\t\t\t: null,\n\t\tsignOut,\n\t\tsignComment,\n\t};\n}\n",
"target": "<%- config.aliases.hooks %>/usemetalensauth.ts"
}
]
}