next-token-auth 1.0.9 → 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.
- package/README.md +39 -6
- package/dist/index-ChpgBFYz.d.mts +91 -0
- package/dist/index-ChpgBFYz.d.ts +91 -0
- package/dist/index.d.mts +6 -132
- package/dist/index.d.ts +6 -132
- package/dist/react/index.d.mts +54 -0
- package/dist/react/index.d.ts +54 -0
- package/dist/react/index.js +613 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/index.mjs +608 -0
- package/dist/react/index.mjs.map +1 -0
- package/dist/server/index.d.mts +80 -0
- package/dist/server/index.d.ts +80 -0
- package/dist/server/index.js +286 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/index.mjs +282 -0
- package/dist/server/index.mjs.map +1 -0
- package/package.json +2 -2
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. ["/", "/
|
|
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
|
-
//
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
type ExpiryInput = number | string;
|
|
2
|
+
type ExpiryStrategy = "backend" | "config" | "hybrid";
|
|
3
|
+
interface AuthTokens {
|
|
4
|
+
accessToken: string;
|
|
5
|
+
refreshToken: string;
|
|
6
|
+
/** Unix timestamp (ms) */
|
|
7
|
+
accessTokenExpiresAt: number;
|
|
8
|
+
/** Unix timestamp (ms) */
|
|
9
|
+
refreshTokenExpiresAt?: number;
|
|
10
|
+
}
|
|
11
|
+
interface AuthSession<User = unknown> {
|
|
12
|
+
user: User | null;
|
|
13
|
+
tokens: AuthTokens | null;
|
|
14
|
+
isAuthenticated: boolean;
|
|
15
|
+
}
|
|
16
|
+
interface LoginInput {
|
|
17
|
+
[key: string]: unknown;
|
|
18
|
+
}
|
|
19
|
+
interface LoginResponse<User = unknown> {
|
|
20
|
+
user: User;
|
|
21
|
+
accessToken: string;
|
|
22
|
+
refreshToken: string;
|
|
23
|
+
/** Seconds until access token expires (legacy field) */
|
|
24
|
+
expiresIn?: number;
|
|
25
|
+
accessTokenExpiresIn?: number | string;
|
|
26
|
+
refreshTokenExpiresIn?: number | string;
|
|
27
|
+
}
|
|
28
|
+
interface AuthConfig<User = unknown> {
|
|
29
|
+
/** Base URL of your backend API */
|
|
30
|
+
baseUrl: string;
|
|
31
|
+
endpoints: {
|
|
32
|
+
login: string;
|
|
33
|
+
register?: string;
|
|
34
|
+
refresh: string;
|
|
35
|
+
logout?: string;
|
|
36
|
+
/** Endpoint to fetch the current user profile */
|
|
37
|
+
me?: string;
|
|
38
|
+
};
|
|
39
|
+
routes?: {
|
|
40
|
+
/** Paths that are always accessible without auth */
|
|
41
|
+
public: string[];
|
|
42
|
+
/** Paths that require authentication */
|
|
43
|
+
protected: string[];
|
|
44
|
+
/**
|
|
45
|
+
* Paths only accessible when NOT authenticated (e.g. /login, /register).
|
|
46
|
+
* Authenticated users are redirected to `redirectAuthenticatedTo`.
|
|
47
|
+
*/
|
|
48
|
+
guestOnly?: string[];
|
|
49
|
+
/**
|
|
50
|
+
* Where to redirect authenticated users who visit a guestOnly route.
|
|
51
|
+
* @default "/dashboard"
|
|
52
|
+
*/
|
|
53
|
+
redirectAuthenticatedTo?: string;
|
|
54
|
+
};
|
|
55
|
+
token: {
|
|
56
|
+
storage: "cookie" | "memory";
|
|
57
|
+
cookieName?: string;
|
|
58
|
+
secure?: boolean;
|
|
59
|
+
sameSite?: "strict" | "lax" | "none";
|
|
60
|
+
};
|
|
61
|
+
/** Secret used for encrypting stored tokens */
|
|
62
|
+
secret: string;
|
|
63
|
+
/** Automatically refresh access token before expiry */
|
|
64
|
+
autoRefresh?: boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Seconds before expiry to trigger a proactive refresh.
|
|
67
|
+
* @default 60
|
|
68
|
+
*/
|
|
69
|
+
refreshThreshold?: number;
|
|
70
|
+
expiry?: {
|
|
71
|
+
accessTokenExpiresIn?: ExpiryInput;
|
|
72
|
+
refreshTokenExpiresIn?: ExpiryInput;
|
|
73
|
+
/**
|
|
74
|
+
* - "backend" → trust expiresIn from API response
|
|
75
|
+
* - "config" → use config values only
|
|
76
|
+
* - "hybrid" → backend first, fallback to config
|
|
77
|
+
* @default "hybrid"
|
|
78
|
+
*/
|
|
79
|
+
strategy?: ExpiryStrategy;
|
|
80
|
+
};
|
|
81
|
+
/** Optional custom fetch implementation */
|
|
82
|
+
fetchFn?: typeof fetch;
|
|
83
|
+
/** Called after a successful login */
|
|
84
|
+
onLogin?: (session: AuthSession<User>) => void;
|
|
85
|
+
/** Called after logout */
|
|
86
|
+
onLogout?: () => void;
|
|
87
|
+
/** Called when token refresh fails */
|
|
88
|
+
onRefreshError?: (error: unknown) => void;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export type { AuthConfig as A, ExpiryInput as E, LoginInput as L, AuthSession as a, AuthTokens as b, ExpiryStrategy as c, LoginResponse as d };
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
type ExpiryInput = number | string;
|
|
2
|
+
type ExpiryStrategy = "backend" | "config" | "hybrid";
|
|
3
|
+
interface AuthTokens {
|
|
4
|
+
accessToken: string;
|
|
5
|
+
refreshToken: string;
|
|
6
|
+
/** Unix timestamp (ms) */
|
|
7
|
+
accessTokenExpiresAt: number;
|
|
8
|
+
/** Unix timestamp (ms) */
|
|
9
|
+
refreshTokenExpiresAt?: number;
|
|
10
|
+
}
|
|
11
|
+
interface AuthSession<User = unknown> {
|
|
12
|
+
user: User | null;
|
|
13
|
+
tokens: AuthTokens | null;
|
|
14
|
+
isAuthenticated: boolean;
|
|
15
|
+
}
|
|
16
|
+
interface LoginInput {
|
|
17
|
+
[key: string]: unknown;
|
|
18
|
+
}
|
|
19
|
+
interface LoginResponse<User = unknown> {
|
|
20
|
+
user: User;
|
|
21
|
+
accessToken: string;
|
|
22
|
+
refreshToken: string;
|
|
23
|
+
/** Seconds until access token expires (legacy field) */
|
|
24
|
+
expiresIn?: number;
|
|
25
|
+
accessTokenExpiresIn?: number | string;
|
|
26
|
+
refreshTokenExpiresIn?: number | string;
|
|
27
|
+
}
|
|
28
|
+
interface AuthConfig<User = unknown> {
|
|
29
|
+
/** Base URL of your backend API */
|
|
30
|
+
baseUrl: string;
|
|
31
|
+
endpoints: {
|
|
32
|
+
login: string;
|
|
33
|
+
register?: string;
|
|
34
|
+
refresh: string;
|
|
35
|
+
logout?: string;
|
|
36
|
+
/** Endpoint to fetch the current user profile */
|
|
37
|
+
me?: string;
|
|
38
|
+
};
|
|
39
|
+
routes?: {
|
|
40
|
+
/** Paths that are always accessible without auth */
|
|
41
|
+
public: string[];
|
|
42
|
+
/** Paths that require authentication */
|
|
43
|
+
protected: string[];
|
|
44
|
+
/**
|
|
45
|
+
* Paths only accessible when NOT authenticated (e.g. /login, /register).
|
|
46
|
+
* Authenticated users are redirected to `redirectAuthenticatedTo`.
|
|
47
|
+
*/
|
|
48
|
+
guestOnly?: string[];
|
|
49
|
+
/**
|
|
50
|
+
* Where to redirect authenticated users who visit a guestOnly route.
|
|
51
|
+
* @default "/dashboard"
|
|
52
|
+
*/
|
|
53
|
+
redirectAuthenticatedTo?: string;
|
|
54
|
+
};
|
|
55
|
+
token: {
|
|
56
|
+
storage: "cookie" | "memory";
|
|
57
|
+
cookieName?: string;
|
|
58
|
+
secure?: boolean;
|
|
59
|
+
sameSite?: "strict" | "lax" | "none";
|
|
60
|
+
};
|
|
61
|
+
/** Secret used for encrypting stored tokens */
|
|
62
|
+
secret: string;
|
|
63
|
+
/** Automatically refresh access token before expiry */
|
|
64
|
+
autoRefresh?: boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Seconds before expiry to trigger a proactive refresh.
|
|
67
|
+
* @default 60
|
|
68
|
+
*/
|
|
69
|
+
refreshThreshold?: number;
|
|
70
|
+
expiry?: {
|
|
71
|
+
accessTokenExpiresIn?: ExpiryInput;
|
|
72
|
+
refreshTokenExpiresIn?: ExpiryInput;
|
|
73
|
+
/**
|
|
74
|
+
* - "backend" → trust expiresIn from API response
|
|
75
|
+
* - "config" → use config values only
|
|
76
|
+
* - "hybrid" → backend first, fallback to config
|
|
77
|
+
* @default "hybrid"
|
|
78
|
+
*/
|
|
79
|
+
strategy?: ExpiryStrategy;
|
|
80
|
+
};
|
|
81
|
+
/** Optional custom fetch implementation */
|
|
82
|
+
fetchFn?: typeof fetch;
|
|
83
|
+
/** Called after a successful login */
|
|
84
|
+
onLogin?: (session: AuthSession<User>) => void;
|
|
85
|
+
/** Called after logout */
|
|
86
|
+
onLogout?: () => void;
|
|
87
|
+
/** Called when token refresh fails */
|
|
88
|
+
onRefreshError?: (error: unknown) => void;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export type { AuthConfig as A, ExpiryInput as E, LoginInput as L, AuthSession as a, AuthTokens as b, ExpiryStrategy as c, LoginResponse as d };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,85 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
interface AuthTokens {
|
|
7
|
-
accessToken: string;
|
|
8
|
-
refreshToken: string;
|
|
9
|
-
/** Unix timestamp (ms) */
|
|
10
|
-
accessTokenExpiresAt: number;
|
|
11
|
-
/** Unix timestamp (ms) */
|
|
12
|
-
refreshTokenExpiresAt?: number;
|
|
13
|
-
}
|
|
14
|
-
interface AuthSession<User = unknown> {
|
|
15
|
-
user: User | null;
|
|
16
|
-
tokens: AuthTokens | null;
|
|
17
|
-
isAuthenticated: boolean;
|
|
18
|
-
}
|
|
19
|
-
interface LoginInput {
|
|
20
|
-
[key: string]: unknown;
|
|
21
|
-
}
|
|
22
|
-
interface LoginResponse<User = unknown> {
|
|
23
|
-
user: User;
|
|
24
|
-
accessToken: string;
|
|
25
|
-
refreshToken: string;
|
|
26
|
-
/** Seconds until access token expires (legacy field) */
|
|
27
|
-
expiresIn?: number;
|
|
28
|
-
accessTokenExpiresIn?: number | string;
|
|
29
|
-
refreshTokenExpiresIn?: number | string;
|
|
30
|
-
}
|
|
31
|
-
interface AuthConfig<User = unknown> {
|
|
32
|
-
/** Base URL of your backend API */
|
|
33
|
-
baseUrl: string;
|
|
34
|
-
endpoints: {
|
|
35
|
-
login: string;
|
|
36
|
-
register?: string;
|
|
37
|
-
refresh: string;
|
|
38
|
-
logout?: string;
|
|
39
|
-
/** Endpoint to fetch the current user profile */
|
|
40
|
-
me?: string;
|
|
41
|
-
};
|
|
42
|
-
routes?: {
|
|
43
|
-
/** Paths that are always accessible without auth */
|
|
44
|
-
public: string[];
|
|
45
|
-
/** Paths that require authentication */
|
|
46
|
-
protected: string[];
|
|
47
|
-
};
|
|
48
|
-
token: {
|
|
49
|
-
storage: "cookie" | "memory";
|
|
50
|
-
cookieName?: string;
|
|
51
|
-
secure?: boolean;
|
|
52
|
-
sameSite?: "strict" | "lax" | "none";
|
|
53
|
-
};
|
|
54
|
-
/** Secret used for encrypting stored tokens */
|
|
55
|
-
secret: string;
|
|
56
|
-
/** Automatically refresh access token before expiry */
|
|
57
|
-
autoRefresh?: boolean;
|
|
58
|
-
/**
|
|
59
|
-
* Seconds before expiry to trigger a proactive refresh.
|
|
60
|
-
* @default 60
|
|
61
|
-
*/
|
|
62
|
-
refreshThreshold?: number;
|
|
63
|
-
expiry?: {
|
|
64
|
-
accessTokenExpiresIn?: ExpiryInput;
|
|
65
|
-
refreshTokenExpiresIn?: ExpiryInput;
|
|
66
|
-
/**
|
|
67
|
-
* - "backend" → trust expiresIn from API response
|
|
68
|
-
* - "config" → use config values only
|
|
69
|
-
* - "hybrid" → backend first, fallback to config
|
|
70
|
-
* @default "hybrid"
|
|
71
|
-
*/
|
|
72
|
-
strategy?: ExpiryStrategy;
|
|
73
|
-
};
|
|
74
|
-
/** Optional custom fetch implementation */
|
|
75
|
-
fetchFn?: typeof fetch;
|
|
76
|
-
/** Called after a successful login */
|
|
77
|
-
onLogin?: (session: AuthSession<User>) => void;
|
|
78
|
-
/** Called after logout */
|
|
79
|
-
onLogout?: () => void;
|
|
80
|
-
/** Called when token refresh fails */
|
|
81
|
-
onRefreshError?: (error: unknown) => void;
|
|
82
|
-
}
|
|
1
|
+
import { A as AuthConfig, b as AuthTokens, a as AuthSession, L as LoginInput, E as ExpiryInput } from './index-ChpgBFYz.mjs';
|
|
2
|
+
export { c as ExpiryStrategy, d as LoginResponse } from './index-ChpgBFYz.mjs';
|
|
3
|
+
export { AuthProvider, UseAuthReturn, UseRequireAuthOptions, useAuth, useRequireAuth, useSession } from './react/index.mjs';
|
|
4
|
+
import 'react/jsx-runtime';
|
|
5
|
+
import 'react';
|
|
83
6
|
|
|
84
7
|
/**
|
|
85
8
|
* Manages storage, retrieval, and expiry checks for auth tokens.
|
|
@@ -203,55 +126,6 @@ declare class AuthClient<User = unknown> {
|
|
|
203
126
|
private notifyListeners;
|
|
204
127
|
}
|
|
205
128
|
|
|
206
|
-
interface AuthProviderProps<User = unknown> {
|
|
207
|
-
config: AuthConfig<User>;
|
|
208
|
-
children: React.ReactNode;
|
|
209
|
-
}
|
|
210
|
-
declare function AuthProvider<User = unknown>({ config, children, }: AuthProviderProps<User>): react_jsx_runtime.JSX.Element;
|
|
211
|
-
|
|
212
|
-
interface UseAuthReturn<User = unknown> {
|
|
213
|
-
session: AuthSession<User>;
|
|
214
|
-
login: (input: LoginInput) => Promise<void>;
|
|
215
|
-
logout: () => Promise<void>;
|
|
216
|
-
refresh: () => Promise<void>;
|
|
217
|
-
isLoading: boolean;
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* Primary hook for authentication operations.
|
|
221
|
-
*
|
|
222
|
-
* @example
|
|
223
|
-
* const { session, login, logout, isLoading } = useAuth();
|
|
224
|
-
*/
|
|
225
|
-
declare function useAuth<User = unknown>(): UseAuthReturn<User>;
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* Returns the current auth session without exposing login/logout actions.
|
|
229
|
-
*
|
|
230
|
-
* @example
|
|
231
|
-
* const { user, isAuthenticated } = useSession();
|
|
232
|
-
*/
|
|
233
|
-
declare function useSession<User = unknown>(): AuthSession<User>;
|
|
234
|
-
|
|
235
|
-
interface UseRequireAuthOptions {
|
|
236
|
-
/** Path to redirect unauthenticated users to. @default "/login" */
|
|
237
|
-
redirectTo?: string;
|
|
238
|
-
/** Called when the user is not authenticated (use for custom redirect logic) */
|
|
239
|
-
onUnauthenticated?: () => void;
|
|
240
|
-
}
|
|
241
|
-
/**
|
|
242
|
-
* Redirects unauthenticated users to the login page.
|
|
243
|
-
* Works with both Next.js App Router and Pages Router.
|
|
244
|
-
*
|
|
245
|
-
* @example
|
|
246
|
-
* // App Router (client component)
|
|
247
|
-
* useRequireAuth({ redirectTo: "/login" });
|
|
248
|
-
*
|
|
249
|
-
* @example
|
|
250
|
-
* // Custom handler
|
|
251
|
-
* useRequireAuth({ onUnauthenticated: () => router.push("/login") });
|
|
252
|
-
*/
|
|
253
|
-
declare function useRequireAuth(options?: UseRequireAuthOptions): void;
|
|
254
|
-
|
|
255
129
|
/**
|
|
256
130
|
* Parses an expiry value into seconds.
|
|
257
131
|
* Accepts:
|
|
@@ -280,4 +154,4 @@ declare function encrypt(data: string, secret: string): Promise<string>;
|
|
|
280
154
|
*/
|
|
281
155
|
declare function decrypt(data: string, secret: string): Promise<string>;
|
|
282
156
|
|
|
283
|
-
export { AuthClient,
|
|
157
|
+
export { AuthClient, AuthConfig, AuthSession, AuthTokens, ExpiryInput, HttpClient, LoginInput, SessionManager, TokenManager, decrypt, encrypt, parseExpiry, safeParseExpiry };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,85 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
interface AuthTokens {
|
|
7
|
-
accessToken: string;
|
|
8
|
-
refreshToken: string;
|
|
9
|
-
/** Unix timestamp (ms) */
|
|
10
|
-
accessTokenExpiresAt: number;
|
|
11
|
-
/** Unix timestamp (ms) */
|
|
12
|
-
refreshTokenExpiresAt?: number;
|
|
13
|
-
}
|
|
14
|
-
interface AuthSession<User = unknown> {
|
|
15
|
-
user: User | null;
|
|
16
|
-
tokens: AuthTokens | null;
|
|
17
|
-
isAuthenticated: boolean;
|
|
18
|
-
}
|
|
19
|
-
interface LoginInput {
|
|
20
|
-
[key: string]: unknown;
|
|
21
|
-
}
|
|
22
|
-
interface LoginResponse<User = unknown> {
|
|
23
|
-
user: User;
|
|
24
|
-
accessToken: string;
|
|
25
|
-
refreshToken: string;
|
|
26
|
-
/** Seconds until access token expires (legacy field) */
|
|
27
|
-
expiresIn?: number;
|
|
28
|
-
accessTokenExpiresIn?: number | string;
|
|
29
|
-
refreshTokenExpiresIn?: number | string;
|
|
30
|
-
}
|
|
31
|
-
interface AuthConfig<User = unknown> {
|
|
32
|
-
/** Base URL of your backend API */
|
|
33
|
-
baseUrl: string;
|
|
34
|
-
endpoints: {
|
|
35
|
-
login: string;
|
|
36
|
-
register?: string;
|
|
37
|
-
refresh: string;
|
|
38
|
-
logout?: string;
|
|
39
|
-
/** Endpoint to fetch the current user profile */
|
|
40
|
-
me?: string;
|
|
41
|
-
};
|
|
42
|
-
routes?: {
|
|
43
|
-
/** Paths that are always accessible without auth */
|
|
44
|
-
public: string[];
|
|
45
|
-
/** Paths that require authentication */
|
|
46
|
-
protected: string[];
|
|
47
|
-
};
|
|
48
|
-
token: {
|
|
49
|
-
storage: "cookie" | "memory";
|
|
50
|
-
cookieName?: string;
|
|
51
|
-
secure?: boolean;
|
|
52
|
-
sameSite?: "strict" | "lax" | "none";
|
|
53
|
-
};
|
|
54
|
-
/** Secret used for encrypting stored tokens */
|
|
55
|
-
secret: string;
|
|
56
|
-
/** Automatically refresh access token before expiry */
|
|
57
|
-
autoRefresh?: boolean;
|
|
58
|
-
/**
|
|
59
|
-
* Seconds before expiry to trigger a proactive refresh.
|
|
60
|
-
* @default 60
|
|
61
|
-
*/
|
|
62
|
-
refreshThreshold?: number;
|
|
63
|
-
expiry?: {
|
|
64
|
-
accessTokenExpiresIn?: ExpiryInput;
|
|
65
|
-
refreshTokenExpiresIn?: ExpiryInput;
|
|
66
|
-
/**
|
|
67
|
-
* - "backend" → trust expiresIn from API response
|
|
68
|
-
* - "config" → use config values only
|
|
69
|
-
* - "hybrid" → backend first, fallback to config
|
|
70
|
-
* @default "hybrid"
|
|
71
|
-
*/
|
|
72
|
-
strategy?: ExpiryStrategy;
|
|
73
|
-
};
|
|
74
|
-
/** Optional custom fetch implementation */
|
|
75
|
-
fetchFn?: typeof fetch;
|
|
76
|
-
/** Called after a successful login */
|
|
77
|
-
onLogin?: (session: AuthSession<User>) => void;
|
|
78
|
-
/** Called after logout */
|
|
79
|
-
onLogout?: () => void;
|
|
80
|
-
/** Called when token refresh fails */
|
|
81
|
-
onRefreshError?: (error: unknown) => void;
|
|
82
|
-
}
|
|
1
|
+
import { A as AuthConfig, b as AuthTokens, a as AuthSession, L as LoginInput, E as ExpiryInput } from './index-ChpgBFYz.js';
|
|
2
|
+
export { c as ExpiryStrategy, d as LoginResponse } from './index-ChpgBFYz.js';
|
|
3
|
+
export { AuthProvider, UseAuthReturn, UseRequireAuthOptions, useAuth, useRequireAuth, useSession } from './react/index.js';
|
|
4
|
+
import 'react/jsx-runtime';
|
|
5
|
+
import 'react';
|
|
83
6
|
|
|
84
7
|
/**
|
|
85
8
|
* Manages storage, retrieval, and expiry checks for auth tokens.
|
|
@@ -203,55 +126,6 @@ declare class AuthClient<User = unknown> {
|
|
|
203
126
|
private notifyListeners;
|
|
204
127
|
}
|
|
205
128
|
|
|
206
|
-
interface AuthProviderProps<User = unknown> {
|
|
207
|
-
config: AuthConfig<User>;
|
|
208
|
-
children: React.ReactNode;
|
|
209
|
-
}
|
|
210
|
-
declare function AuthProvider<User = unknown>({ config, children, }: AuthProviderProps<User>): react_jsx_runtime.JSX.Element;
|
|
211
|
-
|
|
212
|
-
interface UseAuthReturn<User = unknown> {
|
|
213
|
-
session: AuthSession<User>;
|
|
214
|
-
login: (input: LoginInput) => Promise<void>;
|
|
215
|
-
logout: () => Promise<void>;
|
|
216
|
-
refresh: () => Promise<void>;
|
|
217
|
-
isLoading: boolean;
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* Primary hook for authentication operations.
|
|
221
|
-
*
|
|
222
|
-
* @example
|
|
223
|
-
* const { session, login, logout, isLoading } = useAuth();
|
|
224
|
-
*/
|
|
225
|
-
declare function useAuth<User = unknown>(): UseAuthReturn<User>;
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* Returns the current auth session without exposing login/logout actions.
|
|
229
|
-
*
|
|
230
|
-
* @example
|
|
231
|
-
* const { user, isAuthenticated } = useSession();
|
|
232
|
-
*/
|
|
233
|
-
declare function useSession<User = unknown>(): AuthSession<User>;
|
|
234
|
-
|
|
235
|
-
interface UseRequireAuthOptions {
|
|
236
|
-
/** Path to redirect unauthenticated users to. @default "/login" */
|
|
237
|
-
redirectTo?: string;
|
|
238
|
-
/** Called when the user is not authenticated (use for custom redirect logic) */
|
|
239
|
-
onUnauthenticated?: () => void;
|
|
240
|
-
}
|
|
241
|
-
/**
|
|
242
|
-
* Redirects unauthenticated users to the login page.
|
|
243
|
-
* Works with both Next.js App Router and Pages Router.
|
|
244
|
-
*
|
|
245
|
-
* @example
|
|
246
|
-
* // App Router (client component)
|
|
247
|
-
* useRequireAuth({ redirectTo: "/login" });
|
|
248
|
-
*
|
|
249
|
-
* @example
|
|
250
|
-
* // Custom handler
|
|
251
|
-
* useRequireAuth({ onUnauthenticated: () => router.push("/login") });
|
|
252
|
-
*/
|
|
253
|
-
declare function useRequireAuth(options?: UseRequireAuthOptions): void;
|
|
254
|
-
|
|
255
129
|
/**
|
|
256
130
|
* Parses an expiry value into seconds.
|
|
257
131
|
* Accepts:
|
|
@@ -280,4 +154,4 @@ declare function encrypt(data: string, secret: string): Promise<string>;
|
|
|
280
154
|
*/
|
|
281
155
|
declare function decrypt(data: string, secret: string): Promise<string>;
|
|
282
156
|
|
|
283
|
-
export { AuthClient,
|
|
157
|
+
export { AuthClient, AuthConfig, AuthSession, AuthTokens, ExpiryInput, HttpClient, LoginInput, SessionManager, TokenManager, decrypt, encrypt, parseExpiry, safeParseExpiry };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { A as AuthConfig, a as AuthSession, L as LoginInput } from '../index-ChpgBFYz.mjs';
|
|
4
|
+
|
|
5
|
+
interface AuthProviderProps<User = unknown> {
|
|
6
|
+
config: AuthConfig<User>;
|
|
7
|
+
children: React.ReactNode;
|
|
8
|
+
}
|
|
9
|
+
declare function AuthProvider<User = unknown>({ config, children, }: AuthProviderProps<User>): react_jsx_runtime.JSX.Element;
|
|
10
|
+
|
|
11
|
+
interface UseAuthReturn<User = unknown> {
|
|
12
|
+
session: AuthSession<User>;
|
|
13
|
+
login: (input: LoginInput) => Promise<void>;
|
|
14
|
+
logout: () => Promise<void>;
|
|
15
|
+
refresh: () => Promise<void>;
|
|
16
|
+
isLoading: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Primary hook for authentication operations.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const { session, login, logout, isLoading } = useAuth();
|
|
23
|
+
*/
|
|
24
|
+
declare function useAuth<User = unknown>(): UseAuthReturn<User>;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Returns the current auth session without exposing login/logout actions.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* const { user, isAuthenticated } = useSession();
|
|
31
|
+
*/
|
|
32
|
+
declare function useSession<User = unknown>(): AuthSession<User>;
|
|
33
|
+
|
|
34
|
+
interface UseRequireAuthOptions {
|
|
35
|
+
/** Path to redirect unauthenticated users to. @default "/login" */
|
|
36
|
+
redirectTo?: string;
|
|
37
|
+
/** Called when the user is not authenticated (use for custom redirect logic) */
|
|
38
|
+
onUnauthenticated?: () => void;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Redirects unauthenticated users to the login page.
|
|
42
|
+
* Works with both Next.js App Router and Pages Router.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* // App Router (client component)
|
|
46
|
+
* useRequireAuth({ redirectTo: "/login" });
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* // Custom handler
|
|
50
|
+
* useRequireAuth({ onUnauthenticated: () => router.push("/login") });
|
|
51
|
+
*/
|
|
52
|
+
declare function useRequireAuth(options?: UseRequireAuthOptions): void;
|
|
53
|
+
|
|
54
|
+
export { AuthProvider, type UseAuthReturn, type UseRequireAuthOptions, useAuth, useRequireAuth, useSession };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { A as AuthConfig, a as AuthSession, L as LoginInput } from '../index-ChpgBFYz.js';
|
|
4
|
+
|
|
5
|
+
interface AuthProviderProps<User = unknown> {
|
|
6
|
+
config: AuthConfig<User>;
|
|
7
|
+
children: React.ReactNode;
|
|
8
|
+
}
|
|
9
|
+
declare function AuthProvider<User = unknown>({ config, children, }: AuthProviderProps<User>): react_jsx_runtime.JSX.Element;
|
|
10
|
+
|
|
11
|
+
interface UseAuthReturn<User = unknown> {
|
|
12
|
+
session: AuthSession<User>;
|
|
13
|
+
login: (input: LoginInput) => Promise<void>;
|
|
14
|
+
logout: () => Promise<void>;
|
|
15
|
+
refresh: () => Promise<void>;
|
|
16
|
+
isLoading: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Primary hook for authentication operations.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const { session, login, logout, isLoading } = useAuth();
|
|
23
|
+
*/
|
|
24
|
+
declare function useAuth<User = unknown>(): UseAuthReturn<User>;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Returns the current auth session without exposing login/logout actions.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* const { user, isAuthenticated } = useSession();
|
|
31
|
+
*/
|
|
32
|
+
declare function useSession<User = unknown>(): AuthSession<User>;
|
|
33
|
+
|
|
34
|
+
interface UseRequireAuthOptions {
|
|
35
|
+
/** Path to redirect unauthenticated users to. @default "/login" */
|
|
36
|
+
redirectTo?: string;
|
|
37
|
+
/** Called when the user is not authenticated (use for custom redirect logic) */
|
|
38
|
+
onUnauthenticated?: () => void;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Redirects unauthenticated users to the login page.
|
|
42
|
+
* Works with both Next.js App Router and Pages Router.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* // App Router (client component)
|
|
46
|
+
* useRequireAuth({ redirectTo: "/login" });
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* // Custom handler
|
|
50
|
+
* useRequireAuth({ onUnauthenticated: () => router.push("/login") });
|
|
51
|
+
*/
|
|
52
|
+
declare function useRequireAuth(options?: UseRequireAuthOptions): void;
|
|
53
|
+
|
|
54
|
+
export { AuthProvider, type UseAuthReturn, type UseRequireAuthOptions, useAuth, useRequireAuth, useSession };
|