Manage user authentication with Privy and Coinbase Wallet
Learn how to handle authentication flows with Privy and Coinbase Wallet, including both Privy-managed authentication and custom backend verification.
Overview
Privy handles the initial authentication flow, managing user sessions and wallet connections. You can also implement additional authentication layers for enhanced security or custom requirements.The code snippets in this guide are based on the following example project:
Authentication Flow
Privy manages the primary authentication before users enter your application:
Custom Authentication
For additional security or custom authentication requirements, you can implement backend verification using Sign-In with Ethereum (SIWE)
with the Coinbase Wallet SDK.
Setup
Follow the Setup guide to set up Privy with Coinbase Wallet.
Frontend Component (Sign in with Base)
We use the SignInWithBaseButton component from the @base-org/account-ui/react package to make sure
we are following the brand guidelines.
"use client";import { usePrivy } from "@privy-io/react-auth";import Authentication from "@/components/sections/authentication";export default function Dashboard() { const { authenticated } = usePrivy(); if (!authenticated) { return ( <div className="flex min-h-screen items-center justify-center"> <div className="text-center"> <h1 className="text-2xl font-bold mb-4">Access Required</h1> <p className="mb-6">Please authenticate to access the dashboard.</p> <Authentication /> </div> </div> ); } return ( <div className="min-h-screen p-8"> <h1 className="text-3xl font-bold mb-6">Dashboard</h1> <p>Welcome to your authenticated dashboard!</p> {/* Your protected content here */} </div> );}
Backend Implementation
Development Only: This backend implementation is not production-ready. The nonce management system needs proper persistence and security enhancements for production use.
import { NextResponse } from 'next/server';import crypto from 'crypto';import { nonceStore } from '@/lib/nonce-store';export async function GET() { try { const nonce = crypto.randomBytes(16).toString('hex'); nonceStore.add(nonce); return NextResponse.json({ nonce }); } catch (error) { return NextResponse.json( { error: 'Failed to generate nonce' }, { status: 500 } ); }}
import { NextRequest, NextResponse } from 'next/server';import { createPublicClient, http } from 'viem';import { base } from 'viem/chains';import { nonceStore } from '@/lib/nonce-store';const client = createPublicClient({ chain: base, transport: http() });export async function POST(request: NextRequest) { try { const { address, message, signature } = await request.json(); // Extract nonce from SIWE message const nonce = message.match(/Nonce: (\w+)/)?.[1]; if (!nonce || !nonceStore.consume(nonce)) { return NextResponse.json( { error: 'Invalid or reused nonce' }, { status: 400 } ); } // Verify signature using viem const valid = await client.verifyMessage({ address: address as `0x${string}`, message, signature: signature as `0x${string}` }); if (!valid) { return NextResponse.json( { error: 'Invalid signature' }, { status: 401 } ); } return NextResponse.json({ success: true, address, timestamp: new Date().toISOString() }); } catch (error) { return NextResponse.json( { error: 'Internal server error' }, { status: 500 } ); }}
// Simple in-memory nonce store// In production, use Redis or a databaseclass NonceStore { private nonces = new Set<string>(); add(nonce: string): void { this.nonces.add(nonce); } consume(nonce: string): boolean { return this.nonces.delete(nonce); }}export const nonceStore = new NonceStore();
Production Considerations
For production deployments, enhance the backend implementation with:
Persistent storage: Use Redis or a database instead of in-memory storage
Rate limiting: Implement request rate limiting for nonce generation
Session management: Create proper JWT tokens or session cookies