@farcaster/auth-kit

中文文档
0.8.2发布于 2026/02/05MITTypeScriptES Module构建来源已验证近 30 天下载 2.5万

A React library that makes it easy to add Sign In With Farcaster to your application.

原始使用文档
文档目录14 节
  1. Getting Started
  2. Installation
  3. Import
  4. Add Provider
  5. Add the sign in button
  6. Examples
  7. Troubleshooting
  8. Components
  9. AuthKitProvider
  10. SignInButton
  11. Hooks
  12. useSignIn
  13. useProfile
  14. useSignInMessage

@farcaster/auth-kit

A React library that makes it easy to add Sign In With Farcaster to your application.

Getting Started

Installation

Install AuthKit and its peer dependency viem.

npm install @farcaster/auth-kit viem

Note: AuthKit is a React library. If you're using another frontend framework, take a look at the @farcaster/auth-client library instead.

Import

Import styles and functions.

import "@farcaster/auth-kit/styles.css";
import { AuthKitProvider } from "@farcaster/auth-kit";

Add Provider

Wrap your application with AuthKitProvider.

const config = {
  // For a production app, replace this with an Optimism Mainnet
  // RPC URL from a provider like Alchemy or Infura.
  rpcUrl: "https://mainnet.optimism.io",
  domain: "example.com",
  siweUri: "https://example.com/login",
};

const App = () => {
  return <AuthKitProvider config={config}>{/* Your App */}</AuthKitProvider>;
};

Add the sign in button

Then, in your app, import and render the SignInButton component.

import { SignInButton } from "@farcaster/auth-kit";

export const SignIn = ({ nonce }: { nonce: string }) => {
  return <SignInButton nonce={nonce} />;
};

A Sign in with Farcaster button will be rendered. When the user clicks it they will be prompted to complete sign in using their Farcaster wallet application. Once they complete sign in a signed message will be returned to your callback.

Examples

You can find official examples here.

Troubleshooting

Projects using Create React App (CRA) may run into TypeScript version conflicts, as [email protected] expects a peer dependency of TypeScript version ^3.2.1 || ^4, while both viem and AuthKit require >=5.0.4.

To resolve this issue:

  • Install the latest version of TypeScript: npm i typescript -D
  • Add an override for react-scripts to your package.json file, to remove the version ceiling:
"overrides": {
  "react-scripts": {
    "typescript": ">3.2.1"
  }
}

Note: Always be careful with overrides and ensure they are compatible with the packages using them.

Components

AuthKitProvider

Wrap your application in an AuthKitProvider to use Farcaster Auth.

const config = {
  domain: "example.com",
  siweUri: "https://example.com/login",
  rpcUrl: "https://mainnet.optimism.io",
  relay: "https://relay.farcaster.xyz",
};

const App = () => {
  return <AuthKitProvider config={config}>{/* Your App */}</AuthKitProvider>;
};

Props

PropTypeRequiredDescription
configAuthKitConfigYesConfiguration object. See options in the table below.

config options:

ParameterTypeRequiredDescriptionDefault
domainstringYesDomain of your application.None
siweUristringYesA URI identifying your application.None
relaystringNoFarcaster Auth relay server URLhttps://relay.farcaster.xyz
rpcUrlstringNoOptimism RPC server URLhttps://mainnet.optimism.io
versionstringNoFarcaster Auth versionv1

SignInButton

The main component. Renders a "Sign in With Farcaster" button that prompts the user to scan a QR code with their phone on web or redirects directly on a mobile device. You can use a callback prop or a hook to access the user's authentication status and profile information.

import { SignInButton } from "@farcaster/auth-kit";

export const SignIn = ({ nonce }: { nonce: string }) => {
  return (
    <SignInButton
      nonce={nonce}
      onSuccess={({ fid, username }) =>
        console.log(`Hello, ${username}! Your fid is ${fid}.`)
      }
    />
  );
};

Props

PropTypeRequiredDescriptionDefault
timeoutnumberNoRelay server timeout in ms.300000 (5 minutes)
intervalnumberNoRelay server polling interval in ms.1500 (1.5 seconds)
noncestringNoRandom nonce to include in the Sign In With Farcaster message.None
notBeforestringNoTime when the SIWF message becomes valid. ISO 8601 datetime string.None
expirationTimestringNoTime when the SIWF message expires. ISO 8601 datetime string.None
requestIdstringNoAn optional system specific ID to include in the SIWF message.None
onSuccess(res: UseSignInData) => voidNoCallback invoked when sign in is complete and the user is authenticated.None
onStatusResponse(res: UseWatchStatusData) => voidNoCallback invoked when the component receives a status update from the relay server.None
onError(error: AuthClientError) => voidNoError callback function.None
debugbooleanNoRender a debug panel displaying internal AuthKit state.false

Hooks

useSignIn

Hook for signing in a user. Connects to the relay server, generates a QR code and sign in link to present to the user, and polls relay server for wallet signature.

import { QRCode, useSignIn } from "@farcaster/auth-kit";

function App() {
  const {
    signIn,
    url
    data: { username },
    onSuccess: ({ fid }) => console.log("Your fid:", fid);
  } = useSignIn();

  return (
    <div>
      <button onClick={signIn}>Sign In</button>
      {qrCodeUri && (
        <span>
          Scan this: <QRCode uri={url} />
        </span>
      )}
      {username && `Hello, ${username}!`}
    </div>
  );
}

Parameters

ParameterTypeRequiredDescriptionDefault
timeoutnumberNoRelay server timeout in ms.300000 (5 minutes)
intervalnumberNoRelay server polling interval in ms.1500 (1.5 seconds)
noncestringNoRandom nonce to include in the Sign In With Farcaster message.None
notBeforestringNoTime when the SIWF message becomes valid. ISO 8601 datetime string.None
expirationTimestringNoTime when the SIWF message expires. ISO 8601 datetime string.None
requestIdstringNoAn optional system specific ID to include in the SIWF message.None
onSuccess(res: UseSignInData) => voidNoCallback invoked when sign in is complete and the user is authenticated.None
onStatusResponse(res: UseWatchStatusData) => voidNoCallback invoked when the component receives a status update from the relay server.None
onError(error: AuthClientError) => voidNoError callback function.None

Returns

  {
    signIn: () => void;
    reconnect: () => void;
    isSuccess: boolean;
    isPolling: boolean;
    isError: boolean;
    error: AuthClientError;
    channelToken: string;
    url: string;
    qrCodeUri: string;
    data: {
        state: "pending" | "complete";
        nonce: string;
        message: string;
        signature: string;
        fid: number;
        username: string;
        bio: string;
        displayName: string;
        pfpUrl: string;
        custody: Hex;
        verifications: Hex[];
    },
    validSignature: boolean;
  };
ParameterDescription
signInCall this function to connect to the relay and poll for a signature.
reconnectReconnect to the relay and try again. Use in the event of an error.
isSuccessTrue when the relay server returns a valid signature.
isPollingTrue when the relay state is "pending" and the app is polling the relay server for a response.
isErrorTrue when an error has occurred.
errorAuthClientError instance.
channelTokenFarcaster Auth relay channel token.
urlSign in With Farcaster URL to present to the user. Links to Warpcast in v1.
qrcodeUriQR code image data URI encoding url.
data.stateStatus of the sign in request, either "pending" or "complete"
data.nonceRandom nonce used in the SIWE message. If you do not provide a custom nonce, read this value.
data.messageThe generated SIWE message.
data.signatureHex signature produced by the user's Warpcast wallet.
data.fidUser's Farcaster ID.
data.usernameUser's Farcaster username.
data.bioUser's Farcaster bio.
data.displayNameUser's Farcaster display name.
data.pfpUrlUser's Farcaster profile picture URL.
data.custodyUser's Farcaster ID custody address.
data.verificationsUser's verified addresses.
validSignatureTrue when the signature returned by the relay server is valid.

useProfile

Hook for reading information about the authenticated user.

import { useProfile } from "@farcaster/auth-kit";

function App() {
  const {
    isAuthenticated,
    profile: { fid, pfpUrl, username, displayName, bio },
  } = useProfile();

  return (
    <div>
     { isAuthenticated ? <p>Hello, {username}! Your fid is {fid}.<p>  : <p>You're not signed in.</p>}
    </div>
  );
}

Returns

  {
    isAuthenticated: boolean;
    profile: {
        fid: number;
        username: string;
        bio: string;
        displayName: string;
        pfpUrl: string;
    },
  };
ParameterDescription
isAuthenticatedTrue if the user is authenticated.
profile.fidUser's Farcaster ID.
profile.usernameUser's Farcaster username.
profile.bioUser's Farcaster bio.
profile.displayNameUser's Farcaster display name.
profile.pfpUrlUser's Farcaster profile picture URL.
profile.custodyUser's Farcaster ID custody address.
profile.verificationsUser's verified addresses.

useSignInMessage

Hook for reading the SIWE message and signature used to authenticate the user.

import { useSignInMessage } from "@farcaster/auth-kit";

function App() {
  const { message, signature } = useSignInMessage();

  return (
    <div>
      <p>You signed: {message}</p>
      <p>Your signature: {signature}</p>
    </div>
  );
}

Returns

{
  message: string;
  signature: string;
}
ParameterDescription
messageSIWE message signed by the user.
signatureSignature produced by the user's Warpcast wallet.

版本与发布记录

@farcaster/auth-kit 共发布 30 个版本,首次发布于 2024/01/12;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.8.2latest2026/02/059 个月前
0.0.0-canary-20260205001520canary2026/02/059 个月前
0.0.0-canary-202602050006102026/02/059 个月前
0.0.0-canary-202602042349012026/02/059 个月前
0.0.0-canary-202602042332042026/02/059 个月前
0.8.12025/06/061.4 年前

关于 @farcaster/auth-kit 的常见问题

@farcaster/auth-kit 是什么?

A React library that makes it easy to add Sign In With Farcaster to your application.

如何安装 @farcaster/auth-kit?

在项目目录运行 npm install @farcaster/auth-kit,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>= 17)、react-dom(>= 17)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@farcaster/auth-kit 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@farcaster/auth-kit 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@farcaster/auth-kit 可以商用吗?

@farcaster/auth-kit 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@farcaster/auth-kit 的下载量有多少?

根据 npm 官方下载统计,@farcaster/auth-kit 在 2026/10/15 – 2026/11/13 这 30 天内被下载 24,629 次,日均约 821 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@farcaster/auth-kit 还在维护吗?

最新版本 0.8.2 发布于 2026/02/05(9 个月前);至今共发布 30 个版本,其中过去 12 个月发布了 5 个;首次发布于 2024/01/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。