{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "use-login-with-viem",
  "type": "registry:hook",
  "title": "Use Lens Login with Viem",
  "description": "A hook to execute login with the Lens React SDK using the Viem library for wallet interaction.",
  "dependencies": ["@lens-protocol/react@0.0.0-canary-20250820102520", "viem@2.33.0"],
  "files": [
    {
      "path": "registry/new-york/hooks/use-lens-login-with-viem.ts",
      "content": "import { WalletClient } from \"viem\";\nimport { useState } from \"react\";\nimport {\n  Account,\n  AuthenticatedUser,\n  evmAddress,\n  LoginError,\n  SessionClient,\n  UnexpectedError,\n  useLogin,\n  useSwitchAccount,\n} from \"@lens-protocol/react\";\nimport { LensChainId, LensChainTestnetId } from \"@/registry/new-york/lib/lens-utils\";\n\ntype UseLensLoginWithViemReturnType = {\n  execute: (walletClient: WalletClient, account: Account, appAddress?: string) => Promise<AuthenticatedUser | null>;\n  loading: boolean;\n  error: LoginError | null;\n};\n\nexport const useLensLoginWithViem = ({\n  sessionClient,\n}: {\n  sessionClient: SessionClient | null | undefined;\n}): UseLensLoginWithViemReturnType => {\n  const [error, setError] = useState<LoginError | null>(null);\n\n  const { execute: switchAccount, loading: switchingAccounts } = useSwitchAccount();\n  const { execute: executeLogin, loading: loginLoading } = useLogin();\n\n  const execute = async (\n    walletClient: WalletClient,\n    account: Account,\n    appAddress?: string,\n    useTestnet?: boolean,\n  ): Promise<AuthenticatedUser | null> => {\n    if (loginLoading || switchingAccounts) return null;\n\n    const walletAddress = walletClient.account?.address;\n    if (!walletAddress) {\n      setError(new UnexpectedError(\"Wallet client is not available\"));\n      return null;\n    }\n\n    // First attempt to switch accounts, in case of an active session\n    if (sessionClient?.isSessionClient()) {\n      const switchResult = await switchAccount({\n        account: account.address,\n      });\n      if (switchResult.isOk()) {\n        return switchResult.value;\n      }\n    }\n\n    walletClient.switchChain({ id: useTestnet ? LensChainTestnetId : LensChainId });\n\n    const res = await executeLogin({\n      ...(account.owner === walletAddress\n        ? {\n            accountOwner: {\n              owner: evmAddress(walletAddress),\n              account: evmAddress(account.address),\n              ...(appAddress && {\n                app: evmAddress(appAddress),\n              }),\n            },\n          }\n        : {\n            accountManager: {\n              manager: evmAddress(walletAddress),\n              account: evmAddress(account.address),\n              ...(appAddress && {\n                app: evmAddress(appAddress),\n              }),\n            },\n          }),\n      signMessage: async (message: string) => {\n        return walletClient.signMessage({ account: walletAddress, message });\n      },\n    });\n\n    if (res.isErr()) {\n      setError(res.error);\n      return null;\n    }\n\n    return res.value;\n  };\n\n  return {\n    execute,\n    loading: loginLoading,\n    error: error,\n  };\n};\n",
      "type": "registry:hook"
    }
  ]
}
