next-token-auth 1.0.9 → 1.0.10

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.
@@ -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 * as react_jsx_runtime from 'react/jsx-runtime';
2
- import React from 'react';
3
-
4
- type ExpiryInput = number | string;
5
- type ExpiryStrategy = "backend" | "config" | "hybrid";
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, type AuthConfig, AuthProvider, type AuthSession, type AuthTokens, type ExpiryInput, type ExpiryStrategy, HttpClient, type LoginInput, type LoginResponse, SessionManager, TokenManager, type UseAuthReturn, type UseRequireAuthOptions, decrypt, encrypt, parseExpiry, safeParseExpiry, useAuth, useRequireAuth, useSession };
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 * as react_jsx_runtime from 'react/jsx-runtime';
2
- import React from 'react';
3
-
4
- type ExpiryInput = number | string;
5
- type ExpiryStrategy = "backend" | "config" | "hybrid";
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, type AuthConfig, AuthProvider, type AuthSession, type AuthTokens, type ExpiryInput, type ExpiryStrategy, HttpClient, type LoginInput, type LoginResponse, SessionManager, TokenManager, type UseAuthReturn, type UseRequireAuthOptions, decrypt, encrypt, parseExpiry, safeParseExpiry, useAuth, useRequireAuth, useSession };
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 };