formreader-session-timeout 0.2.3 → 0.2.5

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/dist/index.d.ts CHANGED
@@ -17,6 +17,8 @@ interface SessionConfig {
17
17
  refreshEndpoint: string;
18
18
  /** Endpoint to call for logout */
19
19
  logoutEndpoint: string;
20
+ /** Whether to track idle time */
21
+ trackIdleTime?: boolean;
20
22
  /** Whether to show idle warning dialog */
21
23
  showIdleWarning: boolean;
22
24
  /** Time before idle timeout to show warning (milliseconds) */
@@ -27,6 +29,8 @@ interface SessionConfig {
27
29
  onIdle?: () => void;
28
30
  /** Callback when session has expired */
29
31
  onSessionExpired?: () => void;
32
+ /** Callback when user logs out */
33
+ onLogout?: () => void;
30
34
  /** Callback on refresh success */
31
35
  onRefreshSuccess?: () => void;
32
36
  /** Callback on refresh failure */
@@ -41,10 +45,24 @@ interface SessionConfig {
41
45
  data: any;
42
46
  }>;
43
47
  };
44
- /** Custom function to format the refresh request payload. Allows you to define exactly what data is sent to your API. */
45
- refreshPayloadFormatter?: (token: string) => Record<string, any>;
46
- /** Custom function to format the logout request payload. Allows you to define exactly what data is sent to your API. */
47
- logoutPayloadFormatter?: (token: string) => Record<string, any>;
48
+ /** Custom function to format the refresh request payload */
49
+ refreshPayloadFormatter?: (token: string, refreshToken: string) => Record<string, any>;
50
+ /** Custom function to format the logout request payload */
51
+ logoutPayloadFormatter?: (token: string, refreshToken?: string) => Record<string, any>;
52
+ /** Which field in login response contains access token (default: 'access') */
53
+ accessTokenField?: string;
54
+ /** Which field in login response contains refresh token (default: 'refresh') */
55
+ refreshTokenField?: string;
56
+ /** Which field in refresh response contains new access token (default: 'access') */
57
+ refreshAccessTokenField?: string;
58
+ /** Which field in refresh response contains new refresh token (default: 'refresh') */
59
+ refreshRefreshTokenField?: string;
60
+ /** Whether to store refresh token separately (default: true) */
61
+ storeRefreshToken?: boolean;
62
+ /** Whether to validate token on page reload/initialization (default: true) */
63
+ validateOnInit?: boolean;
64
+ /** Custom token validation endpoint for server-side validation */
65
+ tokenValidationEndpoint?: string;
48
66
  }
49
67
  interface JWTPayload {
50
68
  exp: number;
@@ -77,6 +95,7 @@ declare const DEFAULT_SESSION_CONFIG: SessionConfig;
77
95
  /**
78
96
  * Session Manager Service
79
97
  * Manages token refresh, idle tracking, and session lifecycle
98
+ * Supports configurable token response field mapping
80
99
  */
81
100
 
82
101
  declare class SessionManager {
@@ -90,15 +109,16 @@ declare class SessionManager {
90
109
  private requestDeduplication;
91
110
  constructor(config: Partial<SessionConfig>);
92
111
  /**
93
- * Initialize session management
112
+ * Initialize session management with optional login response
113
+ * Extracts and stores tokens from login response using configured field names
94
114
  */
95
- init(): void;
115
+ init(loginResponse?: any): void;
96
116
  /**
97
117
  * Setup token refresh before expiry
98
118
  */
99
119
  private setupTokenRefresh;
100
120
  /**
101
- * Setup idle activity tracking
121
+ * Setup idle tracking with activity listeners
102
122
  */
103
123
  private setupIdleTracking;
104
124
  /**
@@ -106,27 +126,30 @@ declare class SessionManager {
106
126
  */
107
127
  private setupMaxSessionDuration;
108
128
  /**
109
- * Refresh token
129
+ * Refresh token using stored refresh token
110
130
  */
111
131
  refreshToken(): Promise<boolean>;
112
132
  /**
113
- * Logout and cleanup
133
+ * Manual logout
114
134
  */
115
135
  logout(): Promise<void>;
116
136
  /**
117
- * Extend session (reset idle timer)
137
+ * Check if session is active and token is valid
118
138
  */
119
- extendSession(): void;
139
+ isActive(): boolean;
120
140
  /**
121
- * Get current state
141
+ * Validate current session without initializing
122
142
  */
123
- getState(): SessionState;
143
+ validateSession(): {
144
+ isValid: boolean;
145
+ reason?: string;
146
+ };
124
147
  /**
125
- * Get current config
148
+ * Get current state
126
149
  */
127
- getConfig(): SessionConfig;
150
+ getState(): SessionState;
128
151
  /**
129
- * Update config
152
+ * Update config at runtime
130
153
  */
131
154
  updateConfig(newConfig: Partial<SessionConfig>): void;
132
155
  /**
@@ -171,48 +194,75 @@ declare function isTokenExpired(token: string, bufferMs?: number): boolean;
171
194
  */
172
195
  declare function getTimeUntilExpiry(token: string): number;
173
196
  /**
174
- * Extract token from storage
197
+ * Extract token from storage (access token)
175
198
  */
176
199
  declare function getStoredToken(): string | null;
200
+ /**
201
+ * Extract refresh token from storage
202
+ */
203
+ declare function getStoredRefreshToken(): string | null;
177
204
  /**
178
205
  * Store token in appropriate storage
179
206
  */
180
207
  declare function storeToken(token: string, persistent?: boolean): void;
181
208
  /**
182
- * Clear stored token
209
+ * Store refresh token in appropriate storage
210
+ */
211
+ declare function storeRefreshToken(token: string, persistent?: boolean): void;
212
+ /**
213
+ * Clear all tokens from storage
183
214
  */
184
215
  declare function clearToken(): void;
185
216
  /**
186
- * Validate token structure and expiry
217
+ * Validate token structure
187
218
  */
188
- declare function validateToken(token: string): {
189
- valid: boolean;
190
- error?: string;
191
- };
219
+ declare function validateToken(token: string): boolean;
220
+
221
+ /**
222
+ * Authentication utilities for session validation
223
+ */
224
+ /**
225
+ * Check if user is authenticated with valid token
226
+ * This is the function to use in your App.tsx for page reload validation
227
+ */
228
+ declare function isAuthenticated(): boolean;
229
+ /**
230
+ * Initialize session with token validation on page reload
231
+ * Returns a promise that resolves to authentication status
232
+ */
233
+ declare function initializeAuth(config?: any): Promise<boolean>;
234
+ /**
235
+ * Trigger auth change event for cross-tab synchronization
236
+ */
237
+ declare function triggerAuthChange(): void;
192
238
 
193
239
  /**
194
240
  * useSessionTimeout Hook
195
- * React hook for session timeout management in components
241
+ * React hook for managing session timeout
196
242
  */
197
243
 
198
- interface UseSessionTimeoutOptions {
199
- config?: Partial<SessionConfig>;
200
- enabled?: boolean;
201
- onSessionExpiring?: () => void;
202
- onSessionExpired?: () => void;
203
- onIdle?: () => void;
204
- onRefreshSuccess?: () => void;
244
+ interface UseSessionTimeoutOptions extends Partial<SessionConfig> {
245
+ /**
246
+ * Optional login response from your authentication endpoint
247
+ * Used to extract tokens from response if provided
248
+ */
249
+ loginResponse?: any;
250
+ /**
251
+ * Whether to automatically initialize on mount
252
+ */
253
+ autoInit?: boolean;
205
254
  }
206
255
  declare function useSessionTimeout(options?: UseSessionTimeoutOptions): {
207
- sessionState: SessionState;
208
- timeUntilExpiry: any;
256
+ isActive: boolean;
257
+ isIdle: boolean;
258
+ isRefreshing: boolean;
259
+ refreshAttempts: number;
260
+ timeUntilTimeout: number;
209
261
  timeUntilIdle: number;
210
- idleWarningVisible: boolean;
211
- extendSession: () => void;
262
+ error: Error;
212
263
  refreshToken: () => Promise<boolean>;
213
264
  logout: () => Promise<void>;
214
- updateConfig: (newConfig: Partial<SessionConfig>) => void;
215
- manager: SessionManager;
265
+ dismissIdleWarning: () => void;
216
266
  };
217
267
 
218
268
  /**
@@ -221,4 +271,4 @@ declare function useSessionTimeout(options?: UseSessionTimeoutOptions): {
221
271
  */
222
272
  declare function SessionStatus(): react_jsx_runtime.JSX.Element;
223
273
 
224
- export { DEFAULT_SESSION_CONFIG, JWTPayload, RefreshTokenResponse, SessionConfig, SessionManager, SessionState, SessionStatus, TokenInfo, UseSessionTimeoutOptions, clearToken, getSessionManager, getStoredToken, getTimeUntilExpiry, getTokenInfo, isTokenExpired, resetSessionManager, storeToken, useSessionTimeout, validateToken };
274
+ export { DEFAULT_SESSION_CONFIG, JWTPayload, RefreshTokenResponse, SessionConfig, SessionManager, SessionState, SessionStatus, TokenInfo, UseSessionTimeoutOptions, clearToken, getSessionManager, getStoredRefreshToken, getStoredToken, getTimeUntilExpiry, getTokenInfo, initializeAuth, isAuthenticated, isTokenExpired, resetSessionManager, storeRefreshToken, storeToken, triggerAuthChange, useSessionTimeout, validateToken };