@solvapay/next 1.0.0-preview.9 → 1.0.1-preview.2
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/LICENSE.md +21 -0
- package/README.md +271 -20
- package/dist/chunk-F7TBIH6W.js +74 -0
- package/dist/index.cjs +303 -76
- package/dist/index.d.cts +284 -52
- package/dist/index.d.ts +284 -52
- package/dist/index.js +234 -71
- package/dist/middleware.cjs +111 -0
- package/dist/middleware.d.cts +178 -0
- package/dist/middleware.d.ts +178 -0
- package/dist/middleware.js +8 -0
- package/package.json +16 -10
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
+
import { AuthAdapter } from '@solvapay/auth';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Next.js Middleware Helpers
|
|
6
|
+
*
|
|
7
|
+
* Helpers for creating authentication middleware in Next.js.
|
|
8
|
+
* Works with any AuthAdapter implementation (Supabase, custom, etc.)
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Configuration options for authentication middleware
|
|
13
|
+
*/
|
|
14
|
+
interface AuthMiddlewareOptions {
|
|
15
|
+
/**
|
|
16
|
+
* Auth adapter instance to use for extracting user IDs from requests
|
|
17
|
+
* You can use SupabaseAuthAdapter, MockAuthAdapter, or create your own
|
|
18
|
+
*/
|
|
19
|
+
adapter: AuthAdapter;
|
|
20
|
+
/**
|
|
21
|
+
* Public routes that don't require authentication
|
|
22
|
+
* Routes are matched using pathname.startsWith()
|
|
23
|
+
*/
|
|
24
|
+
publicRoutes?: string[];
|
|
25
|
+
/**
|
|
26
|
+
* Header name to store the user ID (default: 'x-user-id')
|
|
27
|
+
*/
|
|
28
|
+
userIdHeader?: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Creates a Next.js middleware function for authentication
|
|
32
|
+
*
|
|
33
|
+
* This helper:
|
|
34
|
+
* 1. Uses the provided AuthAdapter to extract userId from requests
|
|
35
|
+
* 2. Handles public vs protected routes
|
|
36
|
+
* 3. Adds userId to request headers for downstream routes
|
|
37
|
+
* 4. Returns appropriate error responses for auth failures
|
|
38
|
+
*
|
|
39
|
+
* @param options - Configuration options
|
|
40
|
+
* @returns Next.js middleware function (can be exported as `middleware` or `proxy`)
|
|
41
|
+
*
|
|
42
|
+
* @example Next.js 15
|
|
43
|
+
* ```typescript
|
|
44
|
+
* // middleware.ts (at project root)
|
|
45
|
+
* import { createAuthMiddleware } from '@solvapay/next';
|
|
46
|
+
* import { SupabaseAuthAdapter } from '@solvapay/auth/supabase';
|
|
47
|
+
*
|
|
48
|
+
* const adapter = new SupabaseAuthAdapter({
|
|
49
|
+
* jwtSecret: process.env.SUPABASE_JWT_SECRET!,
|
|
50
|
+
* });
|
|
51
|
+
*
|
|
52
|
+
* export const middleware = createAuthMiddleware({
|
|
53
|
+
* adapter,
|
|
54
|
+
* publicRoutes: ['/api/list-plans'],
|
|
55
|
+
* });
|
|
56
|
+
*
|
|
57
|
+
* export const config = {
|
|
58
|
+
* matcher: ['/api/:path*'],
|
|
59
|
+
* };
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @example Next.js 16 with src/ folder
|
|
63
|
+
* ```typescript
|
|
64
|
+
* // src/proxy.ts (in src/ folder, not project root)
|
|
65
|
+
* import { createAuthMiddleware } from '@solvapay/next';
|
|
66
|
+
* import { SupabaseAuthAdapter } from '@solvapay/auth/supabase';
|
|
67
|
+
*
|
|
68
|
+
* const adapter = new SupabaseAuthAdapter({
|
|
69
|
+
* jwtSecret: process.env.SUPABASE_JWT_SECRET!,
|
|
70
|
+
* });
|
|
71
|
+
*
|
|
72
|
+
* // Use 'proxy' export for Next.js 16 (no deprecation warning)
|
|
73
|
+
* export const proxy = createAuthMiddleware({
|
|
74
|
+
* adapter,
|
|
75
|
+
* publicRoutes: ['/api/list-plans'],
|
|
76
|
+
* });
|
|
77
|
+
*
|
|
78
|
+
* export const config = {
|
|
79
|
+
* matcher: ['/api/:path*'],
|
|
80
|
+
* };
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* @example Custom adapter
|
|
84
|
+
* ```typescript
|
|
85
|
+
* import { createAuthMiddleware } from '@solvapay/next';
|
|
86
|
+
* import type { AuthAdapter } from '@solvapay/auth';
|
|
87
|
+
*
|
|
88
|
+
* const myAdapter: AuthAdapter = {
|
|
89
|
+
* async getUserIdFromRequest(req) {
|
|
90
|
+
* // Your custom auth logic
|
|
91
|
+
* return userId;
|
|
92
|
+
* },
|
|
93
|
+
* };
|
|
94
|
+
*
|
|
95
|
+
* export const middleware = createAuthMiddleware({
|
|
96
|
+
* adapter: myAdapter,
|
|
97
|
+
* });
|
|
98
|
+
* ```
|
|
99
|
+
*
|
|
100
|
+
* **File Location Notes:**
|
|
101
|
+
* - **Next.js 15**: Place `middleware.ts` at project root
|
|
102
|
+
* - **Next.js 16 without `src/` folder**: Place `middleware.ts` or `proxy.ts` at project root
|
|
103
|
+
* - **Next.js 16 with `src/` folder**: Place `src/proxy.ts` or `src/middleware.ts` (in `src/` folder, not root)
|
|
104
|
+
*
|
|
105
|
+
* **Note:** Next.js 16 renamed "middleware" to "proxy". You can export the return value as either
|
|
106
|
+
* `middleware` or `proxy` - both work, but `proxy` is recommended to avoid deprecation warnings.
|
|
107
|
+
*/
|
|
108
|
+
declare function createAuthMiddleware(options: AuthMiddlewareOptions): (request: NextRequest) => Promise<NextResponse<unknown>>;
|
|
109
|
+
/**
|
|
110
|
+
* Configuration options for Supabase authentication middleware
|
|
111
|
+
*/
|
|
112
|
+
interface SupabaseAuthMiddlewareOptions {
|
|
113
|
+
/**
|
|
114
|
+
* Supabase JWT secret (from Supabase dashboard: Settings → API → JWT Secret)
|
|
115
|
+
* If not provided, will use SUPABASE_JWT_SECRET environment variable
|
|
116
|
+
*/
|
|
117
|
+
jwtSecret?: string;
|
|
118
|
+
/**
|
|
119
|
+
* Public routes that don't require authentication
|
|
120
|
+
* Routes are matched using pathname.startsWith()
|
|
121
|
+
*/
|
|
122
|
+
publicRoutes?: string[];
|
|
123
|
+
/**
|
|
124
|
+
* Header name to store the user ID (default: 'x-user-id')
|
|
125
|
+
*/
|
|
126
|
+
userIdHeader?: string;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Creates a Next.js middleware function for Supabase authentication
|
|
130
|
+
*
|
|
131
|
+
* Convenience function that creates a SupabaseAuthAdapter and wraps it with createAuthMiddleware.
|
|
132
|
+
* Only use this if you're using Supabase - otherwise use createAuthMiddleware with your own adapter.
|
|
133
|
+
*
|
|
134
|
+
* Uses dynamic import to avoid requiring Supabase as a dependency in @solvapay/next.
|
|
135
|
+
*
|
|
136
|
+
* @param options - Configuration options
|
|
137
|
+
* @returns Next.js middleware function (can be exported as `middleware` or `proxy`)
|
|
138
|
+
*
|
|
139
|
+
* @example Next.js 15
|
|
140
|
+
* ```typescript
|
|
141
|
+
* // middleware.ts (at project root)
|
|
142
|
+
* import { createSupabaseAuthMiddleware } from '@solvapay/next';
|
|
143
|
+
*
|
|
144
|
+
* export const middleware = createSupabaseAuthMiddleware({
|
|
145
|
+
* publicRoutes: ['/api/list-plans'],
|
|
146
|
+
* });
|
|
147
|
+
*
|
|
148
|
+
* export const config = {
|
|
149
|
+
* matcher: ['/api/:path*'],
|
|
150
|
+
* };
|
|
151
|
+
* ```
|
|
152
|
+
*
|
|
153
|
+
* @example Next.js 16 with src/ folder
|
|
154
|
+
* ```typescript
|
|
155
|
+
* // src/proxy.ts (in src/ folder, not project root)
|
|
156
|
+
* import { createSupabaseAuthMiddleware } from '@solvapay/next';
|
|
157
|
+
*
|
|
158
|
+
* // Use 'proxy' export for Next.js 16 (no deprecation warning)
|
|
159
|
+
* export const proxy = createSupabaseAuthMiddleware({
|
|
160
|
+
* publicRoutes: ['/api/list-plans'],
|
|
161
|
+
* });
|
|
162
|
+
*
|
|
163
|
+
* export const config = {
|
|
164
|
+
* matcher: ['/api/:path*'],
|
|
165
|
+
* };
|
|
166
|
+
* ```
|
|
167
|
+
*
|
|
168
|
+
* **File Location Notes:**
|
|
169
|
+
* - **Next.js 15**: Place `middleware.ts` at project root
|
|
170
|
+
* - **Next.js 16 without `src/` folder**: Place `middleware.ts` or `proxy.ts` at project root
|
|
171
|
+
* - **Next.js 16 with `src/` folder**: Place `src/proxy.ts` or `src/middleware.ts` (in `src/` folder, not root)
|
|
172
|
+
*
|
|
173
|
+
* **Note:** Next.js 16 renamed "middleware" to "proxy". You can export the return value as either
|
|
174
|
+
* `middleware` or `proxy` - both work, but `proxy` is recommended to avoid deprecation warnings.
|
|
175
|
+
*/
|
|
176
|
+
declare function createSupabaseAuthMiddleware(options?: SupabaseAuthMiddlewareOptions): (request: NextRequest) => Promise<NextResponse<unknown>>;
|
|
177
|
+
|
|
178
|
+
export { type AuthMiddlewareOptions, type SupabaseAuthMiddlewareOptions, createAuthMiddleware, createSupabaseAuthMiddleware };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solvapay/next",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1-preview.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -10,6 +10,11 @@
|
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
11
|
"import": "./dist/index.js",
|
|
12
12
|
"require": "./dist/index.cjs"
|
|
13
|
+
},
|
|
14
|
+
"./middleware": {
|
|
15
|
+
"types": "./dist/middleware.d.ts",
|
|
16
|
+
"import": "./dist/middleware.js",
|
|
17
|
+
"require": "./dist/middleware.cjs"
|
|
13
18
|
}
|
|
14
19
|
},
|
|
15
20
|
"files": [
|
|
@@ -29,24 +34,25 @@
|
|
|
29
34
|
},
|
|
30
35
|
"sideEffects": false,
|
|
31
36
|
"dependencies": {
|
|
32
|
-
"@solvapay/auth": "1.0.
|
|
33
|
-
"@solvapay/core": "1.0.
|
|
34
|
-
"@solvapay/server": "1.0.
|
|
37
|
+
"@solvapay/auth": "1.0.1-preview.2",
|
|
38
|
+
"@solvapay/core": "1.0.1-preview.2",
|
|
39
|
+
"@solvapay/server": "1.0.1-preview.2"
|
|
35
40
|
},
|
|
36
41
|
"peerDependencies": {
|
|
37
42
|
"next": ">=13.0.0"
|
|
38
43
|
},
|
|
39
44
|
"devDependencies": {
|
|
40
|
-
"next": "^
|
|
41
|
-
"tsup": "^8.
|
|
42
|
-
"typescript": "^5.
|
|
43
|
-
"vitest": "^
|
|
45
|
+
"next": "^16.2.1",
|
|
46
|
+
"tsup": "^8.5.1",
|
|
47
|
+
"typescript": "^5.9.3",
|
|
48
|
+
"vitest": "^4.1.2",
|
|
44
49
|
"@solvapay/test-utils": "0.0.0"
|
|
45
50
|
},
|
|
46
51
|
"scripts": {
|
|
47
|
-
"build": "tsup src/index.ts --format esm,cjs --dts --tsconfig tsconfig.build.json --external next",
|
|
52
|
+
"build": "tsup src/index.ts src/middleware.ts --format esm,cjs --dts --tsconfig tsconfig.build.json --external next",
|
|
48
53
|
"test": "vitest run || exit 0",
|
|
49
54
|
"test:watch": "vitest",
|
|
50
|
-
"lint": "
|
|
55
|
+
"lint": "eslint src",
|
|
56
|
+
"lint:fix": "eslint src --fix"
|
|
51
57
|
}
|
|
52
58
|
}
|