|
| 1 | +// This example is based on the wagmi SIWE tutorial |
| 2 | +// https://wagmi.sh/examples/sign-in-with-ethereum |
| 3 | +import '../styles/global.css'; |
| 4 | +import '@rainbow-me/rainbowkit/styles.css'; |
| 5 | +import type { AppProps } from 'next/app'; |
| 6 | +import { |
| 7 | + RainbowKitProvider, |
| 8 | + getDefaultWallets, |
| 9 | + connectorsForWallets, |
| 10 | + wallet, |
| 11 | + createAuthenticationAdapter, |
| 12 | + RainbowKitAuthenticationProvider, |
| 13 | + AuthenticationStatus, |
| 14 | +} from '@rainbow-me/rainbowkit'; |
| 15 | +import { chain, configureChains, createClient, WagmiConfig } from 'wagmi'; |
| 16 | +import { alchemyProvider } from 'wagmi/providers/alchemy'; |
| 17 | +import { publicProvider } from 'wagmi/providers/public'; |
| 18 | +import { SiweMessage } from 'siwe'; |
| 19 | +import { useEffect, useMemo, useRef, useState } from 'react'; |
| 20 | + |
| 21 | +const { chains, provider, webSocketProvider } = configureChains( |
| 22 | + [ |
| 23 | + chain.mainnet, |
| 24 | + chain.polygon, |
| 25 | + chain.optimism, |
| 26 | + chain.arbitrum, |
| 27 | + ...(process.env.NEXT_PUBLIC_ENABLE_TESTNETS === 'true' |
| 28 | + ? [chain.goerli, chain.kovan, chain.rinkeby, chain.ropsten] |
| 29 | + : []), |
| 30 | + ], |
| 31 | + [ |
| 32 | + alchemyProvider({ apiKey: '_gg7wSSi0KMBsdKnGVfHDueq6xMB9EkC' }), |
| 33 | + publicProvider(), |
| 34 | + ] |
| 35 | +); |
| 36 | + |
| 37 | +const { wallets } = getDefaultWallets({ |
| 38 | + appName: 'RainbowKit demo', |
| 39 | + chains, |
| 40 | +}); |
| 41 | + |
| 42 | +const demoAppInfo = { |
| 43 | + appName: 'Rainbowkit Demo', |
| 44 | +}; |
| 45 | + |
| 46 | +const connectors = connectorsForWallets([ |
| 47 | + ...wallets, |
| 48 | + { |
| 49 | + groupName: 'Other', |
| 50 | + wallets: [ |
| 51 | + wallet.argent({ chains }), |
| 52 | + wallet.trust({ chains }), |
| 53 | + wallet.ledger({ chains }), |
| 54 | + ], |
| 55 | + }, |
| 56 | +]); |
| 57 | + |
| 58 | +const wagmiClient = createClient({ |
| 59 | + autoConnect: true, |
| 60 | + connectors, |
| 61 | + provider, |
| 62 | + webSocketProvider, |
| 63 | +}); |
| 64 | + |
| 65 | +export default function App({ Component, pageProps }: AppProps) { |
| 66 | + const fetchingStatusRef = useRef(false); |
| 67 | + const verifyingRef = useRef(false); |
| 68 | + const [authStatus, setAuthStatus] = useState<AuthenticationStatus>('loading'); |
| 69 | + |
| 70 | + // Fetch user when: |
| 71 | + useEffect(() => { |
| 72 | + const fetchStatus = async () => { |
| 73 | + if (fetchingStatusRef.current || verifyingRef.current) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + fetchingStatusRef.current = true; |
| 78 | + |
| 79 | + try { |
| 80 | + const response = await fetch('/api/me'); |
| 81 | + const json = await response.json(); |
| 82 | + setAuthStatus(json.address ? 'authenticated' : 'unauthenticated'); |
| 83 | + } catch (_error) { |
| 84 | + setAuthStatus('unauthenticated'); |
| 85 | + } finally { |
| 86 | + fetchingStatusRef.current = false; |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + // 1. page loads |
| 91 | + fetchStatus(); |
| 92 | + |
| 93 | + // 2. window is focused (in case user logs out of another window) |
| 94 | + window.addEventListener('focus', fetchStatus); |
| 95 | + return () => window.removeEventListener('focus', fetchStatus); |
| 96 | + }, []); |
| 97 | + |
| 98 | + const authAdapter = useMemo(() => { |
| 99 | + return createAuthenticationAdapter({ |
| 100 | + getNonce: async () => { |
| 101 | + const response = await fetch('/api/nonce'); |
| 102 | + return await response.text(); |
| 103 | + }, |
| 104 | + |
| 105 | + createMessage: ({ nonce, address, chainId }) => { |
| 106 | + return new SiweMessage({ |
| 107 | + domain: window.location.host, |
| 108 | + address, |
| 109 | + statement: 'Sign in with Ethereum to the app.', |
| 110 | + uri: window.location.origin, |
| 111 | + version: '1', |
| 112 | + chainId, |
| 113 | + nonce, |
| 114 | + }); |
| 115 | + }, |
| 116 | + |
| 117 | + getMessageBody: ({ message }) => { |
| 118 | + return message.prepareMessage(); |
| 119 | + }, |
| 120 | + |
| 121 | + verify: async ({ message, signature }) => { |
| 122 | + verifyingRef.current = true; |
| 123 | + |
| 124 | + try { |
| 125 | + const response = await fetch('/api/verify', { |
| 126 | + method: 'POST', |
| 127 | + headers: { 'Content-Type': 'application/json' }, |
| 128 | + body: JSON.stringify({ message, signature }), |
| 129 | + }); |
| 130 | + |
| 131 | + const authenticated = Boolean(response.ok); |
| 132 | + |
| 133 | + if (authenticated) { |
| 134 | + setAuthStatus(authenticated ? 'authenticated' : 'unauthenticated'); |
| 135 | + } |
| 136 | + |
| 137 | + return authenticated; |
| 138 | + } catch (error) { |
| 139 | + return false; |
| 140 | + } finally { |
| 141 | + verifyingRef.current = false; |
| 142 | + } |
| 143 | + }, |
| 144 | + |
| 145 | + signOut: async () => { |
| 146 | + setAuthStatus('unauthenticated'); |
| 147 | + await fetch('/api/logout'); |
| 148 | + }, |
| 149 | + }); |
| 150 | + }, []); |
| 151 | + |
| 152 | + return ( |
| 153 | + <WagmiConfig client={wagmiClient}> |
| 154 | + <RainbowKitAuthenticationProvider |
| 155 | + adapter={authAdapter} |
| 156 | + status={authStatus} |
| 157 | + > |
| 158 | + <RainbowKitProvider appInfo={demoAppInfo} chains={chains}> |
| 159 | + <Component {...pageProps} /> |
| 160 | + </RainbowKitProvider> |
| 161 | + </RainbowKitAuthenticationProvider> |
| 162 | + </WagmiConfig> |
| 163 | + ); |
| 164 | +} |
0 commit comments