0.0.31 • Published 2 years ago

@farcaster/connect-kit v0.0.31

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

@farcaster/connect-kit

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

Getting Started

Installation

Install ConnectKit and its peer dependency viem.

npm install @farcaster/connect-kit viem

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

Import

Import styles and functions.

import "@farcaster/connect-kit/styles.css";
import { ConnectKitProvider } from "@farcaster/connect-kit";

Add Provider

Wrap your application with ConnectKitProvider.

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 (
    <ConnectKitProvider config={config}>{/* Your App */}</ConnectKitProvider>
  );
};

Add the connect button

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

import { ConnectButton } from "@farcaster/connect-kit";

export const Login = ({ nonce }: { nonce: string }) => {
  return <ConnectButton 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.

Components

ConnectKitProvider

Wrap your application in a ConnectKitProvider to use Farcaster Connect.

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

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

Props

PropTypeRequiredDescription
configConnectKitConfigYesConfiguration object. See options in the table below.

config options:

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

ConnectButton

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 { ConnectButton } from "@farcaster/connect-kit";

export const Login = ({ nonce }: { nonce: string }) => {
  return (
    <ConnectButton
      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: ConnectError) => voidNoError callback function.None
debugbooleanNoRender a debug panel displaying internal ConnectKit 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 { useAppClient } from "@farcaster/connect-kit";

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

  return (
    <div>
      <button onClick={signIn}>Sign In</button>
      {qrCodeUri && (
        <span>
          Scan this: <img src={qrCodeUri} />
        </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: ConnectError) => voidNoError callback function.None

Returns

  {
    signIn: () => void;
    reconnect: () => void;
    isSuccess: boolean;
    isPolling: boolean;
    isError: boolean;
    error: ConnectError;
    channelToken: string;
    connectUri: string;
    qrCodeUri: string;
    data: {
        state: "pending" | "complete";
        nonce: string;
        message: string;
        signature: string;
        fid: number;
        username: string;
        bio: string;
        displayName: string;
        pfpUrl: string;
    },
    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.
errorConnectError instance.
channelTokenConnect relay channel token.
connectUriSign in With Farcaster URL to present to the user. Links to Warpcast in v1.
qrcodeUriQR code image data URL encoding connectUri. Links to Warpcast in v1.
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.
validSignatureTrue when the signature returned by the relay server is valid.

useUserData

Hook for reading information about the authenticated user.

import { useUserData } from "@farcaster/connect-kit";

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

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

Returns

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

useSignInMessage

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

import { useUserData } from "@farcaster/connect-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.
0.0.30

2 years ago

0.0.31

2 years ago

0.0.26

2 years ago

0.0.28

2 years ago

0.0.29

2 years ago

0.0.20

2 years ago

0.0.21

2 years ago

0.0.22

2 years ago

0.0.23

2 years ago

0.0.24

2 years ago

0.0.25

2 years ago

0.0.15

2 years ago

0.0.16

2 years ago

0.0.17

2 years ago

0.0.18

2 years ago

0.0.19

2 years ago

0.0.12

2 years ago

0.0.13

2 years ago

0.0.14

2 years ago

0.0.10

2 years ago

0.0.11

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago