Follow the guide below to integrate your authentication provider with Privy.

Implementation

To integrate JWT-based authentication with Privy in your React application, you’ll need to create a custom PrivyProvider wrapper that supplies your auth token to Privy.

Create a custom PrivyProvider wrapper

Create a component that wraps the PrivyProvider with your custom auth configuration:

PrivyAuthProvider.tsx
import { useCallback, PropsWithChildren } from 'react';
import { PrivyProvider } from '@privy-io/react-auth';

// Import your auth provider's hook or state management
import { useAuth } from 'your-auth-provider';

const PrivyAuthProvider: React.FC<PropsWithChildren> = ({ children }) => {
// Get auth details from your provider
const { getToken, isLoading, isAuthenticated } = useAuth();

// Create a callback to get the token
const getCustomToken = useCallback(
    async () => {
        // Your logic to retrieve the JWT token from your auth provider
        try {
            const token = await getToken();
            return token;
        } catch (error) {
            // If there's an error, the user is likely not authenticated
            return undefined;
        }
    },
    [isAuthenticated, getToken], // Re-create when auth state changes
);

return (
    <PrivyProvider
        appId='your-privy-app-id'
        config={{
            customAuth: {
                // Indicates if your auth provider is currently updating auth state
                isLoading: isLoading,
                // Callback to get the user's JWT token
                getCustomAccessToken: getCustomToken,
            },
        }}
    >
        {children}
    </PrivyProvider>
    );
};

export default PrivyAuthProvider;

Integrate the provider with your app

Make sure to nest your custom provider inside your auth provider in your app structure:

App.tsx
import { AuthProvider } from 'your-auth-provider';
import PrivyAuthProvider from './PrivyAuthProvider';

function App() {
return (
    <AuthProvider>
    {/* Our custom wrapper must be nested inside your AuthProvider */}
    <PrivyAuthProvider>
        {/* Your app content */}
        <MainContent />
    </PrivyAuthProvider>
    </AuthProvider>
);
}

export default App;

Accessing User Authentication Status

Once configured, you can access the user’s authentication status through the Privy SDK:

import { usePrivy } from '@privy-io/react-auth';

function MainContent() {
const { user, ready, authenticated } = usePrivy();

if (!ready) {
    return <div>Loading...</div>;
}

if (!authenticated) {
    return <div>Please log in through your authentication provider</div>;
}

return (
    <div>
    <p>Welcome, authenticated user!</p>
    <p>User ID: {user.id}</p>
    </div>
);
}

When using a custom authentication provider, you should not use the Privy login method (from useLogin or usePrivy). Instead, call the login method of your custom provider, and the Privy SDK will automatically synchronize its state.