Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SIWE support for Next.js App Router #318

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ tsconfig.tsbuildinfo
.env.development.local
.env.test.local
.env.production.local
.idea/

npm-debug.log*
yarn-debug.log*
yarn-error.log*
.yarn/cache
.yarn/install-state.gz
.yarn/install-state.gz
3 changes: 3 additions & 0 deletions examples/nextjs-siwe-app-router/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
NEXT_PUBLIC_WALLETCONNECT_PROJECT_ID=
NEXT_PUBLIC_ALCHEMY_KEY=
SESSION_SECRET=
3 changes: 3 additions & 0 deletions examples/nextjs-siwe-app-router/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
35 changes: 35 additions & 0 deletions examples/nextjs-siwe-app-router/.gitignore
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions examples/nextjs-siwe-app-router/README.md
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions examples/nextjs-siwe-app-router/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
webpack: (config) => {
config.externals.push('pino-pretty', 'lokijs', 'encoding')
config.resolve.fallback = { fs: false, net: false, tls: false }
return config
},
}

module.exports = nextConfig
29 changes: 29 additions & 0 deletions examples/nextjs-siwe-app-router/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "nextjs-siwe-app-router",
"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": "^13.4.4",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"siwe": "^2.1.4",
"viem": "^1.1.0",
"wagmi": "^1.1.1"
},
"devDependencies": {
"@types/node": "18.7.18",
"@types/react": "^18.0.6",
"@types/react-dom": "^18.0.2",
"eslint": "8.23.1",
"eslint-config-next": "^13",
"typescript": "^4.9.5"
}
}
6 changes: 6 additions & 0 deletions examples/nextjs-siwe-app-router/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
1 change: 1 addition & 0 deletions examples/nextjs-siwe-app-router/public/next.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/nextjs-siwe-app-router/public/vercel.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { siweServer } from '@/utils/siweServer' // Your path to siweServer.ts

export const GET = siweServer.apiRouteHandler.GET
export const POST = siweServer.apiRouteHandler.POST
Binary file not shown.
27 changes: 27 additions & 0 deletions examples/nextjs-siwe-app-router/src/app/globals.css
Original file line number Diff line number Diff line change
@@ -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));
}
25 changes: 25 additions & 0 deletions examples/nextjs-siwe-app-router/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import ConnectKitConfig from '@/utils/connectkit';
import type { Metadata } from 'next'
import { Inter } from 'next/font/google'
import './globals.css'

const inter = Inter({ subsets: ['latin'] })

export const metadata: Metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
}

export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body className={inter.className}>
<ConnectKitConfig>{children}</ConnectKitConfig>
</body>
</html>
)
}
13 changes: 13 additions & 0 deletions examples/nextjs-siwe-app-router/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use client'

import { ConnectKitButton, useSIWE } from 'connectkit';

export default function Home() {
const { data, isSignedIn, signOut, signIn } = useSIWE();
console.log({ data, isSignedIn, signOut, signIn });
return (
<div className="flex items-center justify-center min-h-screen py-2">
<ConnectKitButton />
</div>
);
}
27 changes: 27 additions & 0 deletions examples/nextjs-siwe-app-router/src/utils/connectkit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use client'

import { PropsWithChildren } from 'react';

import { siweClient } from '@/utils/siweClient';
import { ConnectKitProvider, getDefaultConfig } from 'connectkit';
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 ConnectKitConfig({ children }: PropsWithChildren) {
return (
<WagmiConfig config={config}>
<siweClient.Provider>
<ConnectKitProvider>
{children}
</ConnectKitProvider>
</siweClient.Provider>
</WagmiConfig>
);
}
6 changes: 6 additions & 0 deletions examples/nextjs-siwe-app-router/src/utils/siweClient.ts
Original file line number Diff line number Diff line change
@@ -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
});
11 changes: 11 additions & 0 deletions examples/nextjs-siwe-app-router/src/utils/siweServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { configureServerSideSIWEAppRouter } from 'connectkit-next-siwe';

export const siweServer = configureServerSideSIWEAppRouter({
session: {
cookieName: 'connectkit-next-siwe',
password: process.env.SESSION_SECRET,
cookieOptions: {
secure: process.env.NODE_ENV === 'production',
},
},
});
18 changes: 18 additions & 0 deletions examples/nextjs-siwe-app-router/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -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: [],
}
20 changes: 20 additions & 0 deletions examples/nextjs-siwe-app-router/tailwind.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Config } from 'tailwindcss'

const config: Config = {
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: [],
}
export default config
41 changes: 41 additions & 0 deletions examples/nextjs-siwe-app-router/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"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/*"
]
},
"plugins": [
{
"name": "next"
}
]
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
"exclude": [
"node_modules"
]
}
4 changes: 2 additions & 2 deletions examples/nextjs-siwe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"connectkit": "workspace:packages/connectkit",
"connectkit-next-siwe": "workspace:packages/connectkit-next-siwe",
"next": "12.3.0",
"next": "^13.4.4",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"siwe": "^2.1.4",
Expand All @@ -23,7 +23,7 @@
"@types/react": "^18.0.6",
"@types/react-dom": "^18.0.2",
"eslint": "8.23.1",
"eslint-config-next": "12.3.0",
"eslint-config-next": "^13",
"typescript": "^4.9.5"
}
}
2 changes: 1 addition & 1 deletion examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"dependencies": {
"connectkit": "workspace:packages/connectkit",
"next": "12.3.0",
"next": "13.4.4",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"viem": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/testbench/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"connectkit": "workspace:packages/connectkit",
"connectkit-next-siwe": "workspace:packages/connectkit-next-siwe",
"local-ssl-proxy": "^1.3.0",
"next": "13.0.0",
"next": "13.4.4",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"viem": "^1.0.0",
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
"dev:cra": "yarn workspace cra run dev",
"dev:nextjs": "yarn workspace nextjs run dev",
"dev:nextjs-siwe": "yarn workspace nextjs-siwe run dev",
"dev:nextjs-siwe-app-router": "yarn workspace nextjs-siwe-app-router run dev",
"dev:vite": "yarn workspace vite run dev",
"dev:testbench": "yarn workspace testbench run dev",
"build": "node build.js && 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:nextjs-siwe-app-router": "yarn build && yarn workspace nextjs-siwe-app-router 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",
Expand Down
2 changes: 1 addition & 1 deletion packages/connectkit-next-siwe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
"peerDependencies": {
"connectkit": ">=1.2.0",
"next": ">=12.x",
"next": ">=13.x",
"react": "17.x || 18.x",
"react-dom": "17.x || 18.x",
"siwe": ">=2"
Expand Down
Loading
Loading