dero-auth
React Components

React Components

DeroAuth provides drop-in React components for wallet-based login.

import {
  DeroAuthProvider,
  SignInWithDero,
  useDeroAuth,
  useDeroAuthContext,
} from "dero-auth/react";

DeroAuthProvider

Wraps your app to provide authentication context:

import { DeroAuthProvider } from "dero-auth/react";
 
export default function Layout({ children }) {
  return (
    <DeroAuthProvider
      appName="My App"
      challengeEndpoint="/api/auth/challenge"
      verifyEndpoint="/api/auth/verify"
    >
      {children}
    </DeroAuthProvider>
  );
}
PropTypeDefaultDescription
appNamestringrequiredDisplayed in wallet connection prompt
challengeEndpointstring/api/auth/challengeServer challenge endpoint
verifyEndpointstring/api/auth/verifyServer verify endpoint
childrenReactNoderequiredChild components

SignInWithDero

A pre-built button that handles the entire authentication flow:

import { SignInWithDero } from "dero-auth/react";
 
function LoginPage() {
  return (
    <SignInWithDero
      onSuccess={(session) => console.log("Signed in!", session.address)}
      onError={(error) => console.error("Auth failed:", error)}
    />
  );
}
PropTypeDescription
onSuccess(session) => voidCallback on successful authentication
onError(error: Error) => voidCallback on failure
labelstringButton text (default: "Sign In with DERO")
classNamestringAdditional CSS classes

The button automatically:

  1. Connects to the local wallet via XSWD
  2. Requests a challenge from the server
  3. Asks the user to sign the challenge
  4. Sends the signature to the server for verification
  5. Stores the JWT session

useDeroAuth Hook

For building custom login UIs:

"use client";
import { useDeroAuth } from "dero-auth/react";
 
export default function CustomLogin() {
  const {
    signIn,           // Trigger the auth flow
    signOut,          // Clear the session
    isAuthenticated,  // Boolean: currently signed in
    address,          // DERO address (or null)
    isLoading,        // Boolean: auth in progress
    error,            // Error object (or null)
  } = useDeroAuth();
 
  if (isAuthenticated) {
    return (
      <div>
        <p>Welcome, {address}</p>
        <button onClick={signOut}>Sign Out</button>
      </div>
    );
  }
 
  return (
    <div>
      <button onClick={signIn} disabled={isLoading}>
        {isLoading ? "Connecting wallet..." : "Connect Wallet"}
      </button>
      {error && <p className="text-red-500">{error.message}</p>}
    </div>
  );
}

useDeroAuthContext

Same as useDeroAuth but requires being inside a DeroAuthProvider:

import { useDeroAuthContext } from "dero-auth/react";
 
function UserBadge() {
  const { isAuthenticated, address } = useDeroAuthContext();
 
  if (!isAuthenticated) return null;
 
  return <span>Connected: {address?.slice(0, 12)}...</span>;
}

Use useDeroAuth for standalone usage (no provider needed). Use useDeroAuthContext when you have a DeroAuthProvider wrapping your app and want shared state across components.