Skip to main content
import { SunbreakProvider } from "@tdfc/sunbreak-react";
import { usePrivy, useIdentityToken } from "@privy-io/react-auth";
import { useCallback } from "react";

export function PrivyExample() {
  const { ready, authenticated, user } = usePrivy();
  const { identityToken } = useIdentityToken();

  // Extract the connected wallet address (if any)
  const wallet = user?.wallet?.address?.toLowerCase();

  // Return the latest identity token when requested
  const getIdentity = useCallback(async () => identityToken, [identityToken]);

  // Construct the Privy adapter for the Sunbreak SDK
  const adapter = {
    name: "privy" as const,
    appId: process.env.NEXT_PUBLIC_PRIVY_APP_ID!,
    getToken: getIdentity,
  };

  return (
    <SunbreakProvider
      base={process.env.NEXT_PUBLIC_SUNBREAK_BASE!}
      clientId={process.env.NEXT_PUBLIC_SUNBREAK_CLIENT_ID!}
      wallet={wallet}
      providerAdapter={adapter}
      refreshDeps={[authenticated, wallet, ready]}
    >
      {/* App */}
    </SunbreakProvider>
  );
}

Explanation

Sunbreak requires an identity token, not just an access token. Privy’s useIdentityToken() hook provides a signed JWT that cryptographically binds the user’s wallet address to their authenticated identity. This ensures that the wallet being sent to Sunbreak is verifiably the same one that authenticated through Privy — preventing mismatched or spoofed wallet sessions. The getIdentity function simply returns the current identity token whenever Sunbreak requests it, allowing the SDK to validate wallet ownership securely.