next-token-auth 1.0.10 → 1.0.11

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.
Files changed (2) hide show
  1. package/README.md +39 -6
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -44,7 +44,7 @@ Most projects end up with hundreds of lines of boilerplate before a single featu
44
44
  - 401 → refresh → retry built into the HTTP client
45
45
  - `getServerSession` — read and validate the session in server components and API routes
46
46
  - `withAuth` — higher-order function to protect App Router route handlers
47
- - `authMiddleware` — Next.js middleware factory for edge-level route protection
47
+ - `authMiddleware` — Next.js middleware factory for edge-level route protection with guest-only route support
48
48
  - Flexible expiry parsing: `"15m"`, `"2h"`, `"2d"`, `"7d"`, `"1w"`, or plain seconds
49
49
  - Three expiry strategies: `backend`, `config`, `hybrid`
50
50
  - Fully typed with TypeScript generics for custom user shapes
@@ -97,6 +97,13 @@ export const authConfig: AuthConfig<User> = {
97
97
  me: "/auth/me",
98
98
  },
99
99
 
100
+ routes: {
101
+ public: ["/", "/about"],
102
+ guestOnly: ["/login", "/register"],
103
+ protected: ["/dashboard/*"],
104
+ redirectAuthenticatedTo: "/dashboard",
105
+ },
106
+
100
107
  token: {
101
108
  storage: "cookie",
102
109
  cookieName: "myapp.session",
@@ -194,8 +201,10 @@ interface AuthConfig<User = unknown> {
194
201
  };
195
202
 
196
203
  routes?: {
197
- public: string[]; // always accessible, e.g. ["/", "/login"]
204
+ public: string[]; // always accessible, e.g. ["/", "/about"]
198
205
  protected: string[]; // require auth, supports wildcard: "/dashboard/*"
206
+ guestOnly?: string[]; // accessible only when NOT authenticated, e.g. ["/login", "/register"]
207
+ redirectAuthenticatedTo?: string; // where to send authenticated users who hit a guestOnly route (default: "/dashboard")
199
208
  };
200
209
 
201
210
  token: {
@@ -345,21 +354,45 @@ Unauthenticated requests are redirected to `/login` by default. Pass `{ redirect
345
354
 
346
355
  ### Middleware (Edge Route Protection)
347
356
 
348
- Protect entire route groups at the edge using Next.js middleware:
357
+ Protect entire route groups at the edge using Next.js middleware. The middleware supports three route categories:
358
+
359
+ - `public` — always accessible, no auth check
360
+ - `protected` — requires authentication, redirects to `/login` if not
361
+ - `guestOnly` — accessible only when NOT authenticated (e.g. login, register pages); authenticated users are redirected away
349
362
 
350
363
  ```ts
351
- // middleware.ts (project root)
364
+ // lib/auth.ts
365
+ export const authConfig: AuthConfig = {
366
+ // ...
367
+ routes: {
368
+ public: ["/", "/about"],
369
+ guestOnly: ["/login", "/register"], // authenticated users get redirected away
370
+ protected: ["/dashboard/*", "/settings/*"],
371
+ redirectAuthenticatedTo: "/dashboard", // where to send authenticated users on guestOnly routes
372
+ },
373
+ };
374
+ ```
375
+
376
+ ```ts
377
+ // middleware.ts (project root)
352
378
  import { authMiddleware } from "next-token-auth/server";
353
379
  import { authConfig } from "@/lib/auth";
354
380
 
355
381
  export const middleware = authMiddleware(authConfig);
356
382
 
357
383
  export const config = {
358
- matcher: ["/dashboard/:path*", "/settings/:path*"],
384
+ // Include all routes you want the middleware to run on
385
+ matcher: ["/login", "/register", "/dashboard/:path*", "/settings/:path*"],
359
386
  };
360
387
  ```
361
388
 
362
- The middleware reads the encrypted session cookie, checks whether the refresh token is still valid, and redirects to `/login` if not. Routes listed in `config.routes.public` are always allowed through.
389
+ Route resolution order inside the middleware:
390
+
391
+ 1. `guestOnly` — if authenticated, redirect to `redirectAuthenticatedTo`
392
+ 2. `public` — always allow through
393
+ 3. `protected` — require valid session, redirect to `/login` if missing
394
+
395
+ The `matcher` in `export const config` controls which routes Next.js even runs the middleware on. Any route not in the matcher is ignored entirely, so make sure it covers both your protected and guest-only routes.
363
396
 
364
397
  ---
365
398
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-token-auth",
3
- "version": "1.0.10",
3
+ "version": "1.0.11",
4
4
  "description": "Production-grade authentication library for Next.js (App Router & Pages Router)",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",