@tidecloak/create-nextjs 0.13.9 → 0.13.13
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 +27 -13
- package/package.json +1 -1
- package/template-js-app/package.json +1 -1
- package/template-ts-app/package.json +1 -1
package/README.md
CHANGED
|
@@ -197,16 +197,9 @@ function Dashboard() {
|
|
|
197
197
|
* `<Unauthenticated>`: renders children only when `authenticated === false`
|
|
198
198
|
|
|
199
199
|
|
|
200
|
-
###
|
|
200
|
+
### Route Protection with TideCloak
|
|
201
201
|
|
|
202
|
-
TideCloak
|
|
203
|
-
|
|
204
|
-
* **Pages Router**: Place your `middleware.ts` file at the project root alongside `pages/`. The exported middleware will apply to both page and API routes.
|
|
205
|
-
* **App Router**: Put `middleware.ts` at the project root (or inside `src/`). It integrates with `/app` routes and layouts, protecting both server components and route handlers.
|
|
206
|
-
|
|
207
|
-
#### Installation
|
|
208
|
-
|
|
209
|
-
No additional install-middleware is included in `@tidecloak/nextjs`.
|
|
202
|
+
TideCloak provides server-side route protection for both the **Pages Router** and the **App Router** in Next.js.
|
|
210
203
|
|
|
211
204
|
#### Options
|
|
212
205
|
|
|
@@ -217,14 +210,36 @@ No additional install-middleware is included in `@tidecloak/nextjs`.
|
|
|
217
210
|
* **`onFailure`**<br>`(ctx: { token: string | null }, req: NextRequest) => NextResponse | void`<br>Hook when auth or role check fails; return a `NextResponse` to override.
|
|
218
211
|
* **`onError`**<br>`(err: any, req: NextRequest) => NextResponse`<br>Hook for unexpected errors in middleware logic.
|
|
219
212
|
|
|
220
|
-
####
|
|
213
|
+
#### Next.js 16+ (proxy.ts)
|
|
214
|
+
|
|
215
|
+
Create `proxy.ts` at your project root. Proxy runs on Node.js runtime.
|
|
216
|
+
|
|
217
|
+
```ts
|
|
218
|
+
import { NextResponse } from 'next/server';
|
|
219
|
+
import tidecloakConfig from './tidecloakAdapter.json';
|
|
220
|
+
import { createTideCloakProxy } from '@tidecloak/nextjs/server';
|
|
221
|
+
|
|
222
|
+
export const proxy = createTideCloakProxy({
|
|
223
|
+
config: tidecloakConfig,
|
|
224
|
+
protectedRoutes: {
|
|
225
|
+
'/admin/*': ['admin'],
|
|
226
|
+
'/api/private/*': ['user'],
|
|
227
|
+
},
|
|
228
|
+
onFailure: ({ token }, req) => NextResponse.redirect(new URL('/login', req.url)),
|
|
229
|
+
onError: (err, req) => NextResponse.rewrite(new URL('/error', req.url)),
|
|
230
|
+
});
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
> **Important:** Do NOT add `export const config` to proxy.ts - it's not supported and will cause errors. Proxy files always run on Node.js runtime and don't need a matcher config.
|
|
234
|
+
|
|
235
|
+
#### Next.js 13-15 (middleware.ts)
|
|
221
236
|
|
|
222
|
-
|
|
237
|
+
Create `middleware.ts` at your project root. Middleware runs on Edge runtime.
|
|
223
238
|
|
|
224
239
|
```ts
|
|
225
240
|
import { NextResponse } from 'next/server';
|
|
226
241
|
import tidecloakConfig from './tidecloakAdapter.json';
|
|
227
|
-
import { createTideCloakMiddleware } from '@tidecloak/nextjs/server
|
|
242
|
+
import { createTideCloakMiddleware } from '@tidecloak/nextjs/server';
|
|
228
243
|
|
|
229
244
|
export default createTideCloakMiddleware({
|
|
230
245
|
config: tidecloakConfig,
|
|
@@ -241,7 +256,6 @@ export const config = {
|
|
|
241
256
|
'/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico)).*)',
|
|
242
257
|
'/api/(.*)',
|
|
243
258
|
],
|
|
244
|
-
runtime: 'edge',
|
|
245
259
|
};
|
|
246
260
|
```
|
|
247
261
|
|
package/package.json
CHANGED