@tuwaio/satellite-siwe-next-auth 0.2.6 → 0.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +71 -50
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
A robust connector module for enabling secure Web3 authentication (Sign-In with Ethereum, SIWE) in Next.js App Router using **Iron Session** for state management.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
---
|
|
10
10
|
|
|
11
11
|
## 🏛️ What is `@tuwaio/satellite-siwe-next-auth`?
|
|
12
12
|
|
|
@@ -14,9 +14,9 @@ A robust connector module for enabling secure Web3 authentication (Sign-In with
|
|
|
14
14
|
|
|
15
15
|
It replaces the complexity of traditional NextAuth setup by leveraging **Iron Session** for robust, encrypted, server-side session management, ensuring a seamless and fully decentralized authentication experience.
|
|
16
16
|
|
|
17
|
-
Built on top of **Wagmi/Viem** for signature generation.
|
|
17
|
+
Built on top of **Wagmi/Viem** for signature generation and verification.
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
---
|
|
20
20
|
|
|
21
21
|
## ✨ Key Features
|
|
22
22
|
|
|
@@ -25,24 +25,21 @@ Built on top of **Wagmi/Viem** for signature generation.
|
|
|
25
25
|
- **Auto-Session Management:** Handles automatic re-authentication and session cleanup upon wallet disconnection or address/chain change.
|
|
26
26
|
- **Flexible Configuration:** Allows custom configuration of Iron Session settings (password, cookie name) and support for **Async Hooks** (e.g., `afterVerify`, `afterLogout`).
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
---
|
|
29
29
|
|
|
30
30
|
## 💾 Installation
|
|
31
31
|
|
|
32
32
|
### Requirements
|
|
33
33
|
|
|
34
|
-
- Node.js 20
|
|
34
|
+
- Node.js 20-24
|
|
35
35
|
- TypeScript 5.9+
|
|
36
36
|
- Wagmi v2+
|
|
37
37
|
- Viem v2+
|
|
38
38
|
- Iron Session v8+
|
|
39
39
|
|
|
40
40
|
```bash
|
|
41
|
-
# Using pnpm (recommended)
|
|
41
|
+
# Using pnpm (recommended), but you can use npm, yarn or bun as well
|
|
42
42
|
pnpm add @tuwaio/satellite-siwe-next-auth siwe iron-session wagmi @wagmi/core viem
|
|
43
|
-
|
|
44
|
-
# Using npm
|
|
45
|
-
npm install @tuwaio/satellite-siwe-next-auth siwe iron-session wagmi @wagmi/core viem
|
|
46
43
|
```
|
|
47
44
|
|
|
48
45
|
### Environment Setup
|
|
@@ -54,23 +51,23 @@ This package requires two **private** server environment variables for security:
|
|
|
54
51
|
| `SIWE_SESSION_SECRET` | **Required.** A cryptographically secure secret (minimum 32 characters) used by Iron Session to encrypt the session cookie. |
|
|
55
52
|
| `SIWE_SESSION_URL` | **Required.** The full base URL of your application (e.g., `http://localhost:3000` or `https://myapp.com`). Used for SIWE domain verification. |
|
|
56
53
|
|
|
57
|
-
**Example `.env
|
|
54
|
+
**Example `.env`:**
|
|
58
55
|
|
|
59
56
|
```env
|
|
60
57
|
SIWE_SESSION_SECRET="oowX51fBPYHSVQxPbktPrfM8Lb3Kbeg3oQ6aCKdeLLo="
|
|
61
58
|
SIWE_SESSION_URL="http://localhost:3000"
|
|
62
59
|
```
|
|
63
60
|
|
|
64
|
-
|
|
61
|
+
---
|
|
65
62
|
|
|
66
63
|
## 🚀 Quick Start
|
|
67
64
|
|
|
68
65
|
### 1. Server Setup (API Route)
|
|
69
66
|
|
|
70
|
-
Create the dynamic API route file at **`
|
|
67
|
+
Create the dynamic API route file at **`app/api/siwe/[...siwe]/route.ts`** and export the handler from the package. This handles `/login`, `/logout`, and `/session` requests.
|
|
71
68
|
|
|
72
69
|
```typescript
|
|
73
|
-
//
|
|
70
|
+
// app/api/siwe/[...siwe]/route.ts
|
|
74
71
|
|
|
75
72
|
import { createSiweApiHandler } from '@tuwaio/satellite-siwe-next-auth/server';
|
|
76
73
|
|
|
@@ -81,22 +78,40 @@ const siweApiHandler = createSiweApiHandler();
|
|
|
81
78
|
export const { GET, POST, DELETE } = siweApiHandler;
|
|
82
79
|
```
|
|
83
80
|
|
|
84
|
-
### 2. Client Setup (
|
|
81
|
+
### 2. Client Setup (Providers)
|
|
85
82
|
|
|
86
83
|
Wrap your application in the `SiweNextAuthProvider`. This provider manages the authentication state, session fetching, and handles auto-sign-out/re-authentication on wallet changes.
|
|
87
84
|
|
|
88
85
|
```tsx
|
|
89
86
|
// src/providers/Providers.tsx
|
|
90
87
|
|
|
88
|
+
'use client';
|
|
89
|
+
|
|
90
|
+
import { ReactNode, useState } from 'react';
|
|
91
|
+
import { WagmiProvider, createConfig, http } from 'wagmi';
|
|
92
|
+
import { mainnet, sepolia } from 'viem/chains';
|
|
93
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
91
94
|
import { SiweNextAuthProvider } from '@tuwaio/satellite-siwe-next-auth';
|
|
92
|
-
// ... other imports (WagmiProvider, QueryClientProvider)
|
|
93
95
|
|
|
94
|
-
|
|
96
|
+
// 1. Configure Wagmi
|
|
97
|
+
export const wagmiConfig = createConfig({
|
|
98
|
+
chains: [mainnet, sepolia],
|
|
99
|
+
transports: {
|
|
100
|
+
[mainnet.id]: http(),
|
|
101
|
+
[sepolia.id]: http(),
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// 2. Create Query Client
|
|
106
|
+
const queryClient = new QueryClient();
|
|
107
|
+
|
|
108
|
+
export function Providers({ children }: { children: ReactNode }) {
|
|
95
109
|
return (
|
|
96
110
|
<WagmiProvider config={wagmiConfig}>
|
|
97
111
|
<QueryClientProvider client={queryClient}>
|
|
98
|
-
{/*
|
|
112
|
+
{/* SiweNextAuthProvider must be inside Wagmi and QueryClient providers */}
|
|
99
113
|
<SiweNextAuthProvider
|
|
114
|
+
wagmiConfig={wagmiConfig}
|
|
100
115
|
enabled={true}
|
|
101
116
|
onSignOut={() => console.log('User signed out')}
|
|
102
117
|
onSignIn={(session) => console.log('User signed in:', session)}
|
|
@@ -109,37 +124,43 @@ export function Providers({ children }: { children: React.ReactNode }) {
|
|
|
109
124
|
}
|
|
110
125
|
```
|
|
111
126
|
|
|
112
|
-
### 3.
|
|
127
|
+
### 3. Integrating with Satellite Connect
|
|
113
128
|
|
|
114
|
-
Use the `useSiweAuth` hook to
|
|
129
|
+
Use the `useSiweAuth` hook to integrate SIWE authentication into the `SatelliteConnectProvider`.
|
|
115
130
|
|
|
116
131
|
```tsx
|
|
117
|
-
// src/
|
|
132
|
+
// src/providers/SatelliteSiweProvider.tsx
|
|
133
|
+
|
|
134
|
+
'use client';
|
|
118
135
|
|
|
136
|
+
import { ReactNode } from 'react';
|
|
137
|
+
import { SatelliteConnectProvider, EVMConnectorsWatcher } from '@tuwaio/satellite-react';
|
|
138
|
+
import { satelliteEVMAdapter } from '@tuwaio/satellite-evm';
|
|
119
139
|
import { useSiweAuth } from '@tuwaio/satellite-siwe-next-auth';
|
|
120
|
-
|
|
140
|
+
import { wagmiConfig } from './Providers'; // Your Wagmi config
|
|
141
|
+
|
|
142
|
+
export function SatelliteSiweProvider({ children }: { children: ReactNode }) {
|
|
143
|
+
// Get SIWE auth state and methods
|
|
144
|
+
const { signInWithSiwe, enabled: siweEnabled, isSignedIn, isRejected } = useSiweAuth();
|
|
121
145
|
|
|
122
|
-
export function SatelliteConnectProviders({ children }: { children: React.ReactNode }) {
|
|
123
|
-
const { signInWithSiwe, isSignedIn, isRejected, enabled } = useSiweAuth();
|
|
124
|
-
|
|
125
|
-
// Example usage: Pass signInWithSiwe as the connection handler to a wallet adapter
|
|
126
|
-
// The adapter will call signInWithSiwe() after a wallet connection is established.
|
|
127
146
|
return (
|
|
128
147
|
<SatelliteConnectProvider
|
|
129
|
-
adapter
|
|
130
|
-
|
|
131
|
-
satelliteEVMAdapter(wagmiConfig, enabled ? signInWithSiwe : undefined),
|
|
132
|
-
// ...
|
|
133
|
-
]}
|
|
148
|
+
// Pass the EVM adapter with SIWE integration
|
|
149
|
+
adapter={satelliteEVMAdapter(wagmiConfig, siweEnabled ? signInWithSiwe : undefined)}
|
|
134
150
|
autoConnect={true}
|
|
135
151
|
>
|
|
136
|
-
{/*
|
|
152
|
+
{/* EVMConnectorsWatcher handles disconnections and account changes */}
|
|
153
|
+
<EVMConnectorsWatcher
|
|
154
|
+
wagmiConfig={wagmiConfig}
|
|
155
|
+
siwe={{ isSignedIn, isRejected, enabled: siweEnabled }}
|
|
156
|
+
/>
|
|
157
|
+
{children}
|
|
137
158
|
</SatelliteConnectProvider>
|
|
138
159
|
);
|
|
139
160
|
}
|
|
140
161
|
```
|
|
141
162
|
|
|
142
|
-
|
|
163
|
+
---
|
|
143
164
|
|
|
144
165
|
## ⚙️ Custom Configuration
|
|
145
166
|
|
|
@@ -158,34 +179,34 @@ The `createSiweApiHandler` accepts an optional configuration object to override
|
|
|
158
179
|
### Example Custom Initialization
|
|
159
180
|
|
|
160
181
|
```typescript
|
|
161
|
-
//
|
|
182
|
+
// app/api/siwe/[...siwe]/route.ts
|
|
162
183
|
|
|
163
184
|
import { createSiweApiHandler } from '@tuwaio/satellite-siwe-next-auth/server';
|
|
164
185
|
|
|
165
186
|
const siweApiHandler = createSiweApiHandler({
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
187
|
+
// Custom Session Settings
|
|
188
|
+
session: {
|
|
189
|
+
cookieName: "my_app_session",
|
|
190
|
+
cookieOptions: {
|
|
191
|
+
maxAge: 60 * 60 * 24 * 7, // 7 days
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
// Custom Hooks
|
|
195
|
+
options: {
|
|
196
|
+
afterVerify: async () => {
|
|
197
|
+
// This logic runs on the server side after a valid signature is confirmed.
|
|
198
|
+
console.log("User verified, ready to create DB record.");
|
|
172
199
|
},
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
afterVerify: async () => {
|
|
176
|
-
// This logic runs on the server side after a valid signature is confirmed.
|
|
177
|
-
console.log("User verified, ready to create DB record.");
|
|
178
|
-
},
|
|
179
|
-
afterLogout: () => {
|
|
180
|
-
console.log("User session destroyed.");
|
|
181
|
-
}
|
|
200
|
+
afterLogout: () => {
|
|
201
|
+
console.log("User session destroyed.");
|
|
182
202
|
}
|
|
203
|
+
}
|
|
183
204
|
});
|
|
184
205
|
|
|
185
206
|
export const { GET, POST, DELETE } = siweApiHandler;
|
|
186
207
|
```
|
|
187
208
|
|
|
188
|
-
|
|
209
|
+
---
|
|
189
210
|
|
|
190
211
|
## 🤝 Contributing & Support
|
|
191
212
|
|