diff --git a/README.md b/README.md index 93aa4a62..fb9f8737 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,7 @@ and start the code bundler: ```sh $ yarn dev:connectkit +$ yarn dev:connectkit-next-siwe ``` and then simply select the example you'd like to run: @@ -82,6 +83,7 @@ and then simply select the example you'd like to run: ```sh $ yarn dev:vite # Vite $ yarn dev:nextjs # Next.js +$ yarn dev:nextjs-siwe # Next.js with SIWE $ yarn dev:cra # Create React App ``` diff --git a/examples/nextjs-siwe/.env.example b/examples/nextjs-siwe/.env.example new file mode 100644 index 00000000..46368dbb --- /dev/null +++ b/examples/nextjs-siwe/.env.example @@ -0,0 +1,3 @@ +NEXT_PUBLIC_WALLETCONNECT_PROJECT_ID= +NEXT_PUBLIC_ALCHEMY_KEY= +SESSION_SECRET= diff --git a/examples/nextjs-siwe/.eslintrc.json b/examples/nextjs-siwe/.eslintrc.json new file mode 100644 index 00000000..bffb357a --- /dev/null +++ b/examples/nextjs-siwe/.eslintrc.json @@ -0,0 +1,3 @@ +{ + "extends": "next/core-web-vitals" +} diff --git a/examples/nextjs-siwe/.gitignore b/examples/nextjs-siwe/.gitignore new file mode 100644 index 00000000..8f322f0d --- /dev/null +++ b/examples/nextjs-siwe/.gitignore @@ -0,0 +1,35 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# next.js +/.next/ +/out/ + +# production +/build + +# misc +.DS_Store +*.pem + +# debug +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# local env files +.env*.local + +# vercel +.vercel + +# typescript +*.tsbuildinfo +next-env.d.ts diff --git a/examples/nextjs-siwe/README.md b/examples/nextjs-siwe/README.md new file mode 100644 index 00000000..e8645bfc --- /dev/null +++ b/examples/nextjs-siwe/README.md @@ -0,0 +1,9 @@ +# [Next.js](https://nextjs.org/) + [TypeScript](https://www.typescriptlang.org/) + [SIWE](https://login.xyz/) + ConnectKit Example + +This is a simple example of how to implement ConnectKit with [Next.js](https://nextjs.org/) and [SIWE](https://docs.family.co/connectkit/authentication) in TypeScript. This example is based on the [SIWE with Next.js documentation](https://docs.family.co/connectkit/auth-with-nextjs) provided by Family. + +## Running the example + +- Have a look at the [instructions in the main README](https://github.com/family/connectkit/blob/main/README.md#running-examples-locally) +- Please copy the `.env.example` file to `.env.local` and fill in the values +- You'll want to set up an environment variable called `SESSION_SECRET` — a randomly generated, strong password of at least 32 characters diff --git a/examples/nextjs-siwe/next.config.js b/examples/nextjs-siwe/next.config.js new file mode 100644 index 00000000..a843cbee --- /dev/null +++ b/examples/nextjs-siwe/next.config.js @@ -0,0 +1,6 @@ +/** @type {import('next').NextConfig} */ +const nextConfig = { + reactStrictMode: true, +} + +module.exports = nextConfig diff --git a/examples/nextjs-siwe/package.json b/examples/nextjs-siwe/package.json new file mode 100644 index 00000000..af8678e6 --- /dev/null +++ b/examples/nextjs-siwe/package.json @@ -0,0 +1,29 @@ +{ + "name": "nextjs-siwe", + "version": "0.1.0", + "private": true, + "scripts": { + "dev": "next dev", + "build": "next build", + "start": "next start", + "lint": "next lint" + }, + "dependencies": { + "connectkit": "workspace:packages/connectkit", + "connectkit-next-siwe": "workspace:packages/connectkit-next-siwe", + "next": "12.3.0", + "react": "^18.0.0", + "react-dom": "^18.0.0", + "siwe": "^2.1.4", + "viem": "^1.1.0", + "wagmi": "^1.1.0" + }, + "devDependencies": { + "@types/node": "18.7.18", + "@types/react": "^18.0.6", + "@types/react-dom": "^18.0.2", + "eslint": "8.23.1", + "eslint-config-next": "12.3.0", + "typescript": "^4.9.5" + } +} diff --git a/examples/nextjs-siwe/postcss.config.js b/examples/nextjs-siwe/postcss.config.js new file mode 100644 index 00000000..33ad091d --- /dev/null +++ b/examples/nextjs-siwe/postcss.config.js @@ -0,0 +1,6 @@ +module.exports = { + plugins: { + tailwindcss: {}, + autoprefixer: {}, + }, +} diff --git a/examples/nextjs-siwe/public/favicon.ico b/examples/nextjs-siwe/public/favicon.ico new file mode 100644 index 00000000..718d6fea Binary files /dev/null and b/examples/nextjs-siwe/public/favicon.ico differ diff --git a/examples/nextjs-siwe/public/next.svg b/examples/nextjs-siwe/public/next.svg new file mode 100644 index 00000000..5174b28c --- /dev/null +++ b/examples/nextjs-siwe/public/next.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/examples/nextjs-siwe/public/vercel.svg b/examples/nextjs-siwe/public/vercel.svg new file mode 100644 index 00000000..d2f84222 --- /dev/null +++ b/examples/nextjs-siwe/public/vercel.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/examples/nextjs-siwe/src/pages/_app.tsx b/examples/nextjs-siwe/src/pages/_app.tsx new file mode 100644 index 00000000..beef4a26 --- /dev/null +++ b/examples/nextjs-siwe/src/pages/_app.tsx @@ -0,0 +1,25 @@ +import '@/styles/globals.css'; +import { siweClient } from '@/utils/siweClient'; +import { ConnectKitProvider, getDefaultConfig } from 'connectkit'; +import type { AppProps } from 'next/app'; +import { WagmiConfig, createConfig } from 'wagmi'; + +const config = createConfig( + getDefaultConfig({ + alchemyId: process.env.NEXT_PUBLIC_ALCHEMY_KEY, + walletConnectProjectId: process.env.NEXT_PUBLIC_WALLETCONNECT_PROJECT_ID!, + appName: 'My ConnectKit App', + }) +); + +export default function App({ Component, pageProps }: AppProps) { + return ( + + + + + + + + ); +} diff --git a/examples/nextjs-siwe/src/pages/_document.tsx b/examples/nextjs-siwe/src/pages/_document.tsx new file mode 100644 index 00000000..54e8bf3e --- /dev/null +++ b/examples/nextjs-siwe/src/pages/_document.tsx @@ -0,0 +1,13 @@ +import { Html, Head, Main, NextScript } from 'next/document' + +export default function Document() { + return ( + + + +
+ + + + ) +} diff --git a/examples/nextjs-siwe/src/pages/api/siwe/[...route].ts b/examples/nextjs-siwe/src/pages/api/siwe/[...route].ts new file mode 100644 index 00000000..702c38f7 --- /dev/null +++ b/examples/nextjs-siwe/src/pages/api/siwe/[...route].ts @@ -0,0 +1,3 @@ +import { siweServer } from '@/utils/siweServer' // Your path to siweServer.ts + +export default siweServer.apiRouteHandler diff --git a/examples/nextjs-siwe/src/pages/index.tsx b/examples/nextjs-siwe/src/pages/index.tsx new file mode 100644 index 00000000..2dd93b56 --- /dev/null +++ b/examples/nextjs-siwe/src/pages/index.tsx @@ -0,0 +1,11 @@ +import { ConnectKitButton, useSIWE } from 'connectkit'; + +export default function Home({ address }: { address?: string }) { + const { data, isSignedIn, signOut, signIn } = useSIWE(); + console.log({ data, isSignedIn, signOut, signIn }); + return ( +
+ +
+ ); +} diff --git a/examples/nextjs-siwe/src/styles/globals.css b/examples/nextjs-siwe/src/styles/globals.css new file mode 100644 index 00000000..fd81e885 --- /dev/null +++ b/examples/nextjs-siwe/src/styles/globals.css @@ -0,0 +1,27 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; + +:root { + --foreground-rgb: 0, 0, 0; + --background-start-rgb: 214, 219, 220; + --background-end-rgb: 255, 255, 255; +} + +@media (prefers-color-scheme: dark) { + :root { + --foreground-rgb: 255, 255, 255; + --background-start-rgb: 0, 0, 0; + --background-end-rgb: 0, 0, 0; + } +} + +body { + color: rgb(var(--foreground-rgb)); + background: linear-gradient( + to bottom, + transparent, + rgb(var(--background-end-rgb)) + ) + rgb(var(--background-start-rgb)); +} diff --git a/examples/nextjs-siwe/src/utils/siweClient.ts b/examples/nextjs-siwe/src/utils/siweClient.ts new file mode 100644 index 00000000..a75fa87d --- /dev/null +++ b/examples/nextjs-siwe/src/utils/siweClient.ts @@ -0,0 +1,6 @@ +import { configureClientSIWE } from 'connectkit-next-siwe' + +export const siweClient = configureClientSIWE({ + apiRoutePrefix: '/api/siwe', // Your API route directory + statement: 'Sign In With Ethereum to prove you control this wallet.', // optional +}) diff --git a/examples/nextjs-siwe/src/utils/siweServer.ts b/examples/nextjs-siwe/src/utils/siweServer.ts new file mode 100644 index 00000000..3d453f8c --- /dev/null +++ b/examples/nextjs-siwe/src/utils/siweServer.ts @@ -0,0 +1,11 @@ +import { configureServerSideSIWE } from 'connectkit-next-siwe'; + +export const siweServer = configureServerSideSIWE({ + session: { + cookieName: 'connectkit-next-siwe', + password: process.env.SESSION_SECRET, + cookieOptions: { + secure: process.env.NODE_ENV === 'production', + }, + }, +}); diff --git a/examples/nextjs-siwe/tailwind.config.js b/examples/nextjs-siwe/tailwind.config.js new file mode 100644 index 00000000..d53b2eaa --- /dev/null +++ b/examples/nextjs-siwe/tailwind.config.js @@ -0,0 +1,18 @@ +/** @type {import('tailwindcss').Config} */ +module.exports = { + content: [ + './src/pages/**/*.{js,ts,jsx,tsx,mdx}', + './src/components/**/*.{js,ts,jsx,tsx,mdx}', + './src/app/**/*.{js,ts,jsx,tsx,mdx}', + ], + theme: { + extend: { + backgroundImage: { + 'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))', + 'gradient-conic': + 'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))', + }, + }, + }, + plugins: [], +} diff --git a/examples/nextjs-siwe/tsconfig.json b/examples/nextjs-siwe/tsconfig.json new file mode 100644 index 00000000..61c19abd --- /dev/null +++ b/examples/nextjs-siwe/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "target": "es5", + "lib": ["dom", "dom.iterable", "esnext"], + "allowJs": true, + "skipLibCheck": true, + "strict": true, + "forceConsistentCasingInFileNames": true, + "noEmit": true, + "esModuleInterop": true, + "module": "esnext", + "moduleResolution": "node", + "resolveJsonModule": true, + "isolatedModules": true, + "jsx": "preserve", + "incremental": true, + "paths": { + "@/*": ["./src/*"] + } + }, + "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"], + "exclude": ["node_modules"] +} diff --git a/package.json b/package.json index 99e322a6..d06058b9 100644 --- a/package.json +++ b/package.json @@ -13,12 +13,14 @@ "dev:connectkit-next-siwe": "yarn workspace connectkit-next-siwe run dev", "dev:cra": "yarn workspace cra run dev", "dev:nextjs": "yarn workspace nextjs run dev", + "dev:nextjs-siwe": "yarn workspace nextjs-siwe run dev", "dev:vite": "yarn workspace vite run dev", "dev:testbench": "yarn workspace testbench run dev", "build": "yarn workspace connectkit run build && cp README.md packages/connectkit/README.md", "build:connectkit-next-siwe": "yarn workspace connectkit-next-siwe run build", "build:cra": "yarn build && yarn workspace cra build", "build:nextjs": "yarn build && yarn workspace nextjs build", + "build:nextjs-siwe": "yarn build && yarn workspace nextjs-siwe build", "build:vite": "yarn build && yarn workspace vite build", "build:testbench": "yarn build && yarn build:connectkit-next-siwe && yarn workspace testbench build", "build:ci": "yarn workspaces foreach -R run build", diff --git a/yarn.lock b/yarn.lock index 7148192c..36c86ece 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12863,6 +12863,25 @@ __metadata: languageName: node linkType: hard +"nextjs-siwe@workspace:examples/nextjs-siwe": + version: 0.0.0-use.local + resolution: "nextjs-siwe@workspace:examples/nextjs-siwe" + dependencies: + "@types/node": 18.7.18 + "@types/react": ^18.0.6 + "@types/react-dom": ^18.0.2 + connectkit: "workspace:packages/connectkit" + eslint: 8.23.1 + eslint-config-next: 12.3.0 + next: 12.3.0 + react: ^18.0.0 + react-dom: ^18.0.0 + typescript: ^4.9.5 + viem: ~0.3.19 + wagmi: ^1.0.0 + languageName: unknown + linkType: soft + "nextjs@workspace:examples/nextjs": version: 0.0.0-use.local resolution: "nextjs@workspace:examples/nextjs"