najm-auth 3.0.0 → 3.1.0
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 +100 -11
- package/dist/NajmAuthClient-DqGucYXi.d.ts +120 -0
- package/dist/client/edge.d.ts +3 -18
- package/dist/client/index.d.ts +3 -2
- package/dist/client/react/index.d.ts +2 -1
- package/dist/client/server/index.d.ts +15 -75
- package/dist/client/server/index.js +77 -53
- package/dist/client/server/react.d.ts +48 -0
- package/dist/client/server/react.js +71 -0
- package/dist/client/server/reactClientGuard.d.ts +2 -0
- package/dist/client/server/reactClientGuard.js +4 -0
- package/dist/getSession-BthP85UA.d.ts +68 -0
- package/dist/sessionRecovery-D5Fa0yZ1.d.ts +18 -0
- package/dist/types-BaSfgxqE.d.ts +166 -0
- package/package.json +10 -2
- package/dist/NajmAuthClient-D2fSvQ_H.d.ts +0 -283
|
@@ -1,283 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* User data from the auth server
|
|
3
|
-
*/
|
|
4
|
-
interface AuthUser {
|
|
5
|
-
id: string;
|
|
6
|
-
email: string;
|
|
7
|
-
name?: string;
|
|
8
|
-
role?: string | null;
|
|
9
|
-
permissions?: string[];
|
|
10
|
-
[key: string]: unknown;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Full auth state snapshot
|
|
14
|
-
*/
|
|
15
|
-
interface AuthState {
|
|
16
|
-
user: AuthUser | null;
|
|
17
|
-
accessToken: string | null;
|
|
18
|
-
isAuthenticated: boolean;
|
|
19
|
-
isLoading: boolean;
|
|
20
|
-
roles: string[];
|
|
21
|
-
permissions: string[];
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Decoded JWT payload (client-side, no verification)
|
|
25
|
-
*/
|
|
26
|
-
interface DecodedToken {
|
|
27
|
-
userId: string;
|
|
28
|
-
jti?: string;
|
|
29
|
-
sessionVersion?: number;
|
|
30
|
-
roles?: string[];
|
|
31
|
-
permissions?: string[];
|
|
32
|
-
exp?: number;
|
|
33
|
-
iat?: number;
|
|
34
|
-
[key: string]: unknown;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Server response envelope
|
|
38
|
-
*/
|
|
39
|
-
interface ServerResponse<T = unknown> {
|
|
40
|
-
data: T;
|
|
41
|
-
message?: string;
|
|
42
|
-
status?: string;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Token pair from login/refresh (internal — refresh token is httpOnly cookie)
|
|
46
|
-
*/
|
|
47
|
-
interface TokenPair {
|
|
48
|
-
accessToken: string;
|
|
49
|
-
refreshToken?: string;
|
|
50
|
-
accessTokenExpiresAt?: number;
|
|
51
|
-
refreshTokenExpiresAt?: number;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Login credentials. `identifier` accepts an email address or a phone number;
|
|
55
|
-
* `email` stays supported for existing callers.
|
|
56
|
-
*/
|
|
57
|
-
interface LoginCredentials {
|
|
58
|
-
identifier?: string;
|
|
59
|
-
email?: string;
|
|
60
|
-
password: string;
|
|
61
|
-
/** Persist the auth cookies past the browser closing. */
|
|
62
|
-
rememberMe?: boolean;
|
|
63
|
-
[key: string]: unknown;
|
|
64
|
-
}
|
|
65
|
-
/** The account must replace its credential before it gets a session. */
|
|
66
|
-
interface CredentialSetupPending {
|
|
67
|
-
nextStep: 'credential_setup';
|
|
68
|
-
setupRequired: true;
|
|
69
|
-
purpose: string;
|
|
70
|
-
expiresAt: string;
|
|
71
|
-
}
|
|
72
|
-
interface AuthenticatedLogin {
|
|
73
|
-
nextStep: 'authenticated';
|
|
74
|
-
user: AuthUser;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Login answer. Branch on `nextStep`: `credential_setup` carries no tokens and
|
|
78
|
-
* leaves the client unauthenticated.
|
|
79
|
-
*/
|
|
80
|
-
type LoginResult = AuthenticatedLogin | CredentialSetupPending;
|
|
81
|
-
/**
|
|
82
|
-
* Retry configuration
|
|
83
|
-
*/
|
|
84
|
-
interface RetryConfig {
|
|
85
|
-
maxRetries?: number;
|
|
86
|
-
backoff?: 'exponential' | 'linear';
|
|
87
|
-
baseDelay?: number;
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Auth client configuration
|
|
91
|
-
*/
|
|
92
|
-
interface AuthClientConfig {
|
|
93
|
-
/** API base URL (e.g., '/api' or 'https://api.example.com') */
|
|
94
|
-
baseURL: string;
|
|
95
|
-
/** Auth endpoints prefix (default: '/auth') */
|
|
96
|
-
authPrefix?: string;
|
|
97
|
-
/** Proactive refresh at this fraction of token lifetime (default: 0.8) */
|
|
98
|
-
refreshThreshold?: number;
|
|
99
|
-
/** Enable multi-tab sync via BroadcastChannel (default: true) */
|
|
100
|
-
tabSync?: boolean;
|
|
101
|
-
/** BroadcastChannel name (default: 'najm-auth') */
|
|
102
|
-
channelName?: string;
|
|
103
|
-
/** Network retry configuration */
|
|
104
|
-
retry?: RetryConfig;
|
|
105
|
-
/** Request timeout in milliseconds (default: 30000) */
|
|
106
|
-
timeout?: number;
|
|
107
|
-
}
|
|
108
|
-
type OAuthProvider = 'google';
|
|
109
|
-
interface OAuthLoginOptions {
|
|
110
|
-
/** Same-origin frontend path after OAuth completes. */
|
|
111
|
-
returnTo?: string;
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Auth event types
|
|
115
|
-
*/
|
|
116
|
-
interface AuthEventMap {
|
|
117
|
-
login: AuthUser;
|
|
118
|
-
logout: null;
|
|
119
|
-
/** Emitted when server-side logout invalidation fails (state was already cleared) */
|
|
120
|
-
logoutError: unknown;
|
|
121
|
-
tokenRefresh: null;
|
|
122
|
-
sessionExpired: null;
|
|
123
|
-
stateChange: AuthState;
|
|
124
|
-
userUpdated: AuthUser;
|
|
125
|
-
}
|
|
126
|
-
type AuthEvent = keyof AuthEventMap;
|
|
127
|
-
type AuthEventHandler<K extends AuthEvent = AuthEvent> = (data: AuthEventMap[K]) => void;
|
|
128
|
-
/**
|
|
129
|
-
* Tab sync message types
|
|
130
|
-
*/
|
|
131
|
-
type TabSyncMessage = {
|
|
132
|
-
type: 'logout';
|
|
133
|
-
} | {
|
|
134
|
-
type: 'sync';
|
|
135
|
-
state: SyncPayload;
|
|
136
|
-
};
|
|
137
|
-
interface SyncPayload {
|
|
138
|
-
accessToken: string | null;
|
|
139
|
-
user: AuthUser | null;
|
|
140
|
-
roles: string[];
|
|
141
|
-
permissions: string[];
|
|
142
|
-
isAuthenticated: boolean;
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* FetchClient request options
|
|
146
|
-
*/
|
|
147
|
-
interface RequestOptions {
|
|
148
|
-
body?: unknown;
|
|
149
|
-
headers?: Record<string, string>;
|
|
150
|
-
signal?: AbortSignal;
|
|
151
|
-
timeout?: number;
|
|
152
|
-
/** Skip auth header attachment (for public endpoints like /login, /register) */
|
|
153
|
-
skipAuth?: boolean;
|
|
154
|
-
/** @internal Prevents 401-refresh loop after a single retry */
|
|
155
|
-
_retried?: boolean;
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Auth error thrown by the client
|
|
159
|
-
*/
|
|
160
|
-
declare class AuthError extends Error {
|
|
161
|
-
status: number;
|
|
162
|
-
body?: unknown;
|
|
163
|
-
constructor(status: number, message: string, body?: unknown);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
interface FetchClientConfig {
|
|
167
|
-
baseURL: string;
|
|
168
|
-
credentials?: RequestCredentials;
|
|
169
|
-
timeout?: number;
|
|
170
|
-
getToken?: () => string | null;
|
|
171
|
-
onUnauthorized?: () => Promise<string | null>;
|
|
172
|
-
retry?: RetryConfig;
|
|
173
|
-
defaultHeaders?: Record<string, string>;
|
|
174
|
-
}
|
|
175
|
-
declare class FetchClient {
|
|
176
|
-
private config;
|
|
177
|
-
constructor(config: FetchClientConfig);
|
|
178
|
-
get<T>(path: string, opts?: RequestOptions): Promise<T>;
|
|
179
|
-
post<T>(path: string, opts?: RequestOptions): Promise<T>;
|
|
180
|
-
put<T>(path: string, opts?: RequestOptions): Promise<T>;
|
|
181
|
-
patch<T>(path: string, opts?: RequestOptions): Promise<T>;
|
|
182
|
-
delete<T>(path: string, opts?: RequestOptions): Promise<T>;
|
|
183
|
-
private request;
|
|
184
|
-
private execute;
|
|
185
|
-
private doFetch;
|
|
186
|
-
private parseBody;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
interface HydrateSession {
|
|
190
|
-
user: AuthUser | null;
|
|
191
|
-
accessToken?: string | null;
|
|
192
|
-
roles?: string[];
|
|
193
|
-
permissions?: string[];
|
|
194
|
-
}
|
|
195
|
-
declare class NajmAuthClient {
|
|
196
|
-
private config;
|
|
197
|
-
private static readonly MAX_REFRESH_FAILURES;
|
|
198
|
-
private static readonly CIRCUIT_RESET_MS;
|
|
199
|
-
private state;
|
|
200
|
-
private refreshTimer;
|
|
201
|
-
private refreshCircuitTimer;
|
|
202
|
-
private refreshPromise;
|
|
203
|
-
private fetchUserPromise;
|
|
204
|
-
private refreshFailures;
|
|
205
|
-
private _hydrated;
|
|
206
|
-
private listeners;
|
|
207
|
-
private eventListeners;
|
|
208
|
-
private tabSync;
|
|
209
|
-
api: FetchClient;
|
|
210
|
-
private readonly prefix;
|
|
211
|
-
private readonly threshold;
|
|
212
|
-
constructor(config: AuthClientConfig);
|
|
213
|
-
login(credentials: LoginCredentials): Promise<LoginResult>;
|
|
214
|
-
register(data: Record<string, unknown>): Promise<AuthUser>;
|
|
215
|
-
getOAuthLoginUrl(provider: OAuthProvider, options?: OAuthLoginOptions): string;
|
|
216
|
-
loginWithOAuth(provider: OAuthProvider, options?: OAuthLoginOptions): void;
|
|
217
|
-
loginWithGoogle(options?: OAuthLoginOptions): void;
|
|
218
|
-
linkOAuthAccount(provider: OAuthProvider, options?: OAuthLoginOptions): Promise<void>;
|
|
219
|
-
completeOAuthLogin(): Promise<AuthUser>;
|
|
220
|
-
logout(): Promise<void>;
|
|
221
|
-
refresh(): Promise<void>;
|
|
222
|
-
fetchUser(): Promise<AuthUser | null>;
|
|
223
|
-
private _doFetchUser;
|
|
224
|
-
forgotPassword(data: {
|
|
225
|
-
email: string;
|
|
226
|
-
}): Promise<void>;
|
|
227
|
-
changePassword(data: {
|
|
228
|
-
currentPassword: string;
|
|
229
|
-
newPassword: string;
|
|
230
|
-
}): Promise<void>;
|
|
231
|
-
resetPassword(data: {
|
|
232
|
-
token: string;
|
|
233
|
-
newPassword: string;
|
|
234
|
-
}): Promise<void>;
|
|
235
|
-
can(permission: string): boolean;
|
|
236
|
-
hasRole(role: string): boolean;
|
|
237
|
-
hasAnyRole(roles: string[]): boolean;
|
|
238
|
-
hasPermission(permission: string): boolean;
|
|
239
|
-
getUser(): AuthUser | null;
|
|
240
|
-
getAccessToken(): string | null;
|
|
241
|
-
isAuthenticated(): boolean;
|
|
242
|
-
getState(): AuthState;
|
|
243
|
-
/**
|
|
244
|
-
* Hydrate the client with a session resolved server-side.
|
|
245
|
-
* Use to skip the initial loading flicker on SSR-rendered pages.
|
|
246
|
-
* Safe to call multiple times — subsequent calls are no-ops.
|
|
247
|
-
*/
|
|
248
|
-
hydrate(session: HydrateSession | null): void;
|
|
249
|
-
isHydrated(): boolean;
|
|
250
|
-
/**
|
|
251
|
-
* A fresh client with the same config — unhydrated, and without tab sync.
|
|
252
|
-
*
|
|
253
|
-
* Server rendering needs one client per request. A single process serves
|
|
254
|
-
* every user, so the hydration latch on a shared client would otherwise pin
|
|
255
|
-
* every later render to the first request's session.
|
|
256
|
-
*/
|
|
257
|
-
fork(): NajmAuthClient;
|
|
258
|
-
on<K extends AuthEvent>(event: K, handler: AuthEventHandler<K>): void;
|
|
259
|
-
off<K extends AuthEvent>(event: K, handler: AuthEventHandler<K>): void;
|
|
260
|
-
subscribe(listener: (state: AuthState) => void): () => void;
|
|
261
|
-
destroy(): void;
|
|
262
|
-
private _refreshWithCircuit;
|
|
263
|
-
private _doRefresh;
|
|
264
|
-
private handleUnauthorized;
|
|
265
|
-
private applyTokens;
|
|
266
|
-
private scheduleRefresh;
|
|
267
|
-
private clearRefreshTimer;
|
|
268
|
-
private clearRefreshCircuitTimer;
|
|
269
|
-
private resetRefreshFailures;
|
|
270
|
-
private registerRefreshFailure;
|
|
271
|
-
private resetState;
|
|
272
|
-
private getSyncPayload;
|
|
273
|
-
private handleTabMessage;
|
|
274
|
-
private notify;
|
|
275
|
-
private emit;
|
|
276
|
-
private validateReturnTo;
|
|
277
|
-
}
|
|
278
|
-
/**
|
|
279
|
-
* Factory function to create an auth client.
|
|
280
|
-
*/
|
|
281
|
-
declare function createAuthClient(config: AuthClientConfig): NajmAuthClient;
|
|
282
|
-
|
|
283
|
-
export { type AuthClientConfig as A, type CredentialSetupPending as C, type DecodedToken as D, FetchClient as F, type HydrateSession as H, type LoginCredentials as L, NajmAuthClient as N, type OAuthLoginOptions as O, type RequestOptions as R, type SyncPayload as S, type TabSyncMessage as T, AuthError as a, type AuthEvent as b, type AuthEventHandler as c, type AuthEventMap as d, type AuthState as e, type AuthUser as f, type AuthenticatedLogin as g, type LoginResult as h, type OAuthProvider as i, type RetryConfig as j, type ServerResponse as k, type TokenPair as l, createAuthClient as m };
|