Custom Privy Authentication Provider with Wagmi Integration
This custom implementation combines Privy authentication with Wagmi for enhanced Web3 functionality in your Next.js application. It includes support for multiple chains, embedded wallets, and various login methods.
To use the Privy provider in your Next.js application, you’ll need to wrap your app with the PrivyProvider component.
First, create a new file called PrivyProvider.tsx in your project:
PrivyProvider.tsx
Copy
"use client";import { PrivyClientConfig, PrivyProvider } from "@privy-io/react-auth";import { sepolia, polygonAmoy, polygon, fuse } from "viem/chains";import { WagmiProvider, createConfig } from "@privy-io/wagmi";import { QueryClient, QueryClientProvider } from "@tanstack/react-query";import { http } from "wagmi";const handleLogin = (user: any) => { console.log(`User ${user.id} logged in!`);};const wagmiConfig = createConfig({ chains: [fuse], //We are using fuse for this example transports: { [fuse.id]: http(), },});const queryClient = new QueryClient();const privyConfig: PrivyClientConfig = { embeddedWallets: { createOnLogin: "users-without-wallets", noPromptOnSignature: false, }, loginMethods: ["wallet", "email", "google"], appearance: { showWalletLoginFirst: true, theme: "light", accentColor: "#676FFF", logo: "https://jiffyscan-frontend.vercel.app/images/Frame%2021.svg", }, supportedChains: [ sepolia, polygonAmoy, polygon, fuse, // Add any other supported chains here ],};export const PrivyProvider = ({ children }: { children: React.ReactNode }) => { return ( <PrivyProvider appId={process.env.NEXT_PUBLIC_PRIVY_APP_ID as string} onSuccess={handleLogin} config={privyConfig} > <QueryClientProvider client={queryClient}> <WagmiProvider config={wagmiConfig} reconnectOnMount={false}> {children} </WagmiProvider> </QueryClientProvider> </PrivyProvider> );};
Then, in your app/layout.tsx file (for Next.js 13+ with App Router), wrap your app with the PrivyProvider:
Embedded Wallets: Created for users without wallets
Login Methods: Supports wallet, email, and Google login
Appearance: Customized theme and logo
Supported Chains: Includes Sepolia, Polygon Amoy, Polygon, and Fuse