@nauth-toolkit/client 0.1.95 → 0.1.97

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.mts CHANGED
@@ -1843,13 +1843,13 @@ declare class NAuthClientError extends Error implements NAuthError {
1843
1843
  * Events are emitted throughout the authentication lifecycle,
1844
1844
  * allowing applications to react to auth state changes.
1845
1845
  */
1846
- type AuthEventType = 'auth:login' | 'auth:signup' | 'auth:success' | 'auth:challenge' | 'auth:error' | 'auth:logout' | 'auth:refresh' | 'oauth:started' | 'oauth:callback' | 'oauth:completed' | 'oauth:error';
1846
+ type AuthEventType = 'auth:login' | 'auth:signup' | 'auth:success' | 'auth:challenge' | 'auth:error' | 'auth:logout' | 'auth:refresh' | 'auth:session_expired' | 'oauth:started' | 'oauth:callback' | 'oauth:completed' | 'oauth:error';
1847
1847
  /**
1848
1848
  * Discriminated union of all authentication events with type-safe payloads
1849
1849
  *
1850
1850
  * Each event has a specific payload type for better type safety.
1851
1851
  */
1852
- type AuthEvent = AuthLoginEvent | AuthSignupEvent | AuthSuccessEvent | AuthChallengeEvent | AuthErrorEvent | AuthLogoutEvent | AuthRefreshEvent | OAuthStartedEvent | OAuthCallbackEvent | OAuthCompletedEvent | OAuthErrorEvent;
1852
+ type AuthEvent = AuthLoginEvent | AuthSignupEvent | AuthSuccessEvent | AuthChallengeEvent | AuthErrorEvent | AuthLogoutEvent | AuthRefreshEvent | AuthSessionExpiredEvent | OAuthStartedEvent | OAuthCallbackEvent | OAuthCompletedEvent | OAuthErrorEvent;
1853
1853
  /**
1854
1854
  * Login initiated event
1855
1855
  */
@@ -1915,6 +1915,17 @@ interface AuthRefreshEvent {
1915
1915
  };
1916
1916
  timestamp: number;
1917
1917
  }
1918
+ /**
1919
+ * Session expired event (refresh token expired or invalid)
1920
+ *
1921
+ * Emitted when token refresh fails with 401, indicating the session
1922
+ * has expired and the user needs to re-authenticate.
1923
+ */
1924
+ interface AuthSessionExpiredEvent {
1925
+ type: 'auth:session_expired';
1926
+ data: Record<string, never>;
1927
+ timestamp: number;
1928
+ }
1918
1929
  /**
1919
1930
  * OAuth flow started event
1920
1931
  */
@@ -2701,6 +2712,18 @@ declare class NAuthClient {
2701
2712
  signup(payload: SignupRequest): Promise<AuthResponse>;
2702
2713
  /**
2703
2714
  * Refresh tokens manually.
2715
+ *
2716
+ * @throws {NAuthClientError} When refresh fails (e.g., session expired)
2717
+ *
2718
+ * @example
2719
+ * ```typescript
2720
+ * try {
2721
+ * await client.refreshTokens();
2722
+ * } catch (error) {
2723
+ * // Session expired - user is already logged out automatically
2724
+ * router.navigate(['/login']);
2725
+ * }
2726
+ * ```
2704
2727
  */
2705
2728
  refreshTokens(): Promise<TokenResponse>;
2706
2729
  /**
@@ -3312,4 +3335,4 @@ declare class FetchAdapter implements HttpAdapter {
3312
3335
  request<T>(config: HttpRequest): Promise<HttpResponse<T>>;
3313
3336
  }
3314
3337
 
3315
- export { type AdminAuditHistoryRequest, AdminOperations, type AdminResetPasswordRequest, type AdminResetPasswordResponse, type AdminSignupRequest, type AdminSignupResponse, type AdminSignupSocialRequest, type AdminSignupSocialResponse, type AuditHistoryResponse, type AuthAuditEvent, type AuthAuditEventStatus, AuthAuditEventType, AuthChallenge, type AuthChallengeEvent, type AuthErrorEvent, type AuthEvent, type AuthEventListener, type AuthEventType, type AuthLoginEvent, type AuthLogoutEvent, type AuthRefreshEvent, type AuthResponse, type AuthResponseContext, type AuthSignupEvent, type AuthSuccessEvent, type AuthUser, type AuthUserSummary, type BackupCodesResponse, type BaseChallengeResponse, BrowserStorage, type ChallengeResponse, ChallengeRouter, type ChangePasswordRequest, type ConfirmForgotPasswordRequest, type ConfirmForgotPasswordResponse, type DateFilter, type DeleteUserResponse, type DisableUserResponse, type EnableUserResponse, EventEmitter, FetchAdapter, type ForceChangePasswordResponse, type ForgotPasswordRequest, type ForgotPasswordResponse, type GetChallengeDataRequest, type GetChallengeDataResponse, type GetSetupDataRequest, type GetSetupDataResponse, type GetUserSessionsResponse, type GetUsersRequest, type GetUsersResponse, type HttpAdapter, type HttpRequest, type HttpResponse, InMemoryStorage, type LinkedAccountsResponse, type LoginRequest, type LogoutAllRequest, type LogoutRequest, type MFAChallengeMethod, type MFACodeResponse, type MFADevice, type MFADeviceMethod, type MFAMethod, type MFAPasskeyResponse, type MFASetupData, type MFASetupResponse, type MFAStatus, type MfaRoutesConfig, type NAuthAdminEndpoints, NAuthClient, type NAuthClientConfig, NAuthClientError, type NAuthEndpoints, type NAuthError, NAuthErrorCode, type NAuthRedirectsConfig, type NAuthStorageAdapter, type OAuthCallbackEvent, type OAuthCompletedEvent, type OAuthErrorEvent, type OAuthStartedEvent, type RecaptchaConfig, type RecaptchaVersion, type ResendCodeRequest, type ResetPasswordWithCodeRequest, type ResetPasswordWithCodeResponse, type ResolvedNAuthClientConfig, type SignupRequest, type SocialLoginOptions, type SocialProvider, type SocialVerifyRequest, type TokenDeliveryMode, type TokenResponse, type UpdateProfileRequest, type UserSessionInfo, type VerifyEmailResponse, type VerifyPhoneCodeResponse, type VerifyPhoneCollectResponse, defaultAdminEndpoints, defaultEndpoints, getChallengeInstructions, getMFAMethod, getMaskedDestination, isOTPChallenge, requiresPhoneCollection, resolveConfig };
3338
+ export { type AdminAuditHistoryRequest, AdminOperations, type AdminResetPasswordRequest, type AdminResetPasswordResponse, type AdminSignupRequest, type AdminSignupResponse, type AdminSignupSocialRequest, type AdminSignupSocialResponse, type AuditHistoryResponse, type AuthAuditEvent, type AuthAuditEventStatus, AuthAuditEventType, AuthChallenge, type AuthChallengeEvent, type AuthErrorEvent, type AuthEvent, type AuthEventListener, type AuthEventType, type AuthLoginEvent, type AuthLogoutEvent, type AuthRefreshEvent, type AuthResponse, type AuthResponseContext, type AuthSessionExpiredEvent, type AuthSignupEvent, type AuthSuccessEvent, type AuthUser, type AuthUserSummary, type BackupCodesResponse, type BaseChallengeResponse, BrowserStorage, type ChallengeResponse, ChallengeRouter, type ChangePasswordRequest, type ConfirmForgotPasswordRequest, type ConfirmForgotPasswordResponse, type DateFilter, type DeleteUserResponse, type DisableUserResponse, type EnableUserResponse, EventEmitter, FetchAdapter, type ForceChangePasswordResponse, type ForgotPasswordRequest, type ForgotPasswordResponse, type GetChallengeDataRequest, type GetChallengeDataResponse, type GetSetupDataRequest, type GetSetupDataResponse, type GetUserSessionsResponse, type GetUsersRequest, type GetUsersResponse, type HttpAdapter, type HttpRequest, type HttpResponse, InMemoryStorage, type LinkedAccountsResponse, type LoginRequest, type LogoutAllRequest, type LogoutRequest, type MFAChallengeMethod, type MFACodeResponse, type MFADevice, type MFADeviceMethod, type MFAMethod, type MFAPasskeyResponse, type MFASetupData, type MFASetupResponse, type MFAStatus, type MfaRoutesConfig, type NAuthAdminEndpoints, NAuthClient, type NAuthClientConfig, NAuthClientError, type NAuthEndpoints, type NAuthError, NAuthErrorCode, type NAuthRedirectsConfig, type NAuthStorageAdapter, type OAuthCallbackEvent, type OAuthCompletedEvent, type OAuthErrorEvent, type OAuthStartedEvent, type RecaptchaConfig, type RecaptchaVersion, type ResendCodeRequest, type ResetPasswordWithCodeRequest, type ResetPasswordWithCodeResponse, type ResolvedNAuthClientConfig, type SignupRequest, type SocialLoginOptions, type SocialProvider, type SocialVerifyRequest, type TokenDeliveryMode, type TokenResponse, type UpdateProfileRequest, type UserSessionInfo, type VerifyEmailResponse, type VerifyPhoneCodeResponse, type VerifyPhoneCollectResponse, defaultAdminEndpoints, defaultEndpoints, getChallengeInstructions, getMFAMethod, getMaskedDestination, isOTPChallenge, requiresPhoneCollection, resolveConfig };
package/dist/index.d.ts CHANGED
@@ -1843,13 +1843,13 @@ declare class NAuthClientError extends Error implements NAuthError {
1843
1843
  * Events are emitted throughout the authentication lifecycle,
1844
1844
  * allowing applications to react to auth state changes.
1845
1845
  */
1846
- type AuthEventType = 'auth:login' | 'auth:signup' | 'auth:success' | 'auth:challenge' | 'auth:error' | 'auth:logout' | 'auth:refresh' | 'oauth:started' | 'oauth:callback' | 'oauth:completed' | 'oauth:error';
1846
+ type AuthEventType = 'auth:login' | 'auth:signup' | 'auth:success' | 'auth:challenge' | 'auth:error' | 'auth:logout' | 'auth:refresh' | 'auth:session_expired' | 'oauth:started' | 'oauth:callback' | 'oauth:completed' | 'oauth:error';
1847
1847
  /**
1848
1848
  * Discriminated union of all authentication events with type-safe payloads
1849
1849
  *
1850
1850
  * Each event has a specific payload type for better type safety.
1851
1851
  */
1852
- type AuthEvent = AuthLoginEvent | AuthSignupEvent | AuthSuccessEvent | AuthChallengeEvent | AuthErrorEvent | AuthLogoutEvent | AuthRefreshEvent | OAuthStartedEvent | OAuthCallbackEvent | OAuthCompletedEvent | OAuthErrorEvent;
1852
+ type AuthEvent = AuthLoginEvent | AuthSignupEvent | AuthSuccessEvent | AuthChallengeEvent | AuthErrorEvent | AuthLogoutEvent | AuthRefreshEvent | AuthSessionExpiredEvent | OAuthStartedEvent | OAuthCallbackEvent | OAuthCompletedEvent | OAuthErrorEvent;
1853
1853
  /**
1854
1854
  * Login initiated event
1855
1855
  */
@@ -1915,6 +1915,17 @@ interface AuthRefreshEvent {
1915
1915
  };
1916
1916
  timestamp: number;
1917
1917
  }
1918
+ /**
1919
+ * Session expired event (refresh token expired or invalid)
1920
+ *
1921
+ * Emitted when token refresh fails with 401, indicating the session
1922
+ * has expired and the user needs to re-authenticate.
1923
+ */
1924
+ interface AuthSessionExpiredEvent {
1925
+ type: 'auth:session_expired';
1926
+ data: Record<string, never>;
1927
+ timestamp: number;
1928
+ }
1918
1929
  /**
1919
1930
  * OAuth flow started event
1920
1931
  */
@@ -2701,6 +2712,18 @@ declare class NAuthClient {
2701
2712
  signup(payload: SignupRequest): Promise<AuthResponse>;
2702
2713
  /**
2703
2714
  * Refresh tokens manually.
2715
+ *
2716
+ * @throws {NAuthClientError} When refresh fails (e.g., session expired)
2717
+ *
2718
+ * @example
2719
+ * ```typescript
2720
+ * try {
2721
+ * await client.refreshTokens();
2722
+ * } catch (error) {
2723
+ * // Session expired - user is already logged out automatically
2724
+ * router.navigate(['/login']);
2725
+ * }
2726
+ * ```
2704
2727
  */
2705
2728
  refreshTokens(): Promise<TokenResponse>;
2706
2729
  /**
@@ -3312,4 +3335,4 @@ declare class FetchAdapter implements HttpAdapter {
3312
3335
  request<T>(config: HttpRequest): Promise<HttpResponse<T>>;
3313
3336
  }
3314
3337
 
3315
- export { type AdminAuditHistoryRequest, AdminOperations, type AdminResetPasswordRequest, type AdminResetPasswordResponse, type AdminSignupRequest, type AdminSignupResponse, type AdminSignupSocialRequest, type AdminSignupSocialResponse, type AuditHistoryResponse, type AuthAuditEvent, type AuthAuditEventStatus, AuthAuditEventType, AuthChallenge, type AuthChallengeEvent, type AuthErrorEvent, type AuthEvent, type AuthEventListener, type AuthEventType, type AuthLoginEvent, type AuthLogoutEvent, type AuthRefreshEvent, type AuthResponse, type AuthResponseContext, type AuthSignupEvent, type AuthSuccessEvent, type AuthUser, type AuthUserSummary, type BackupCodesResponse, type BaseChallengeResponse, BrowserStorage, type ChallengeResponse, ChallengeRouter, type ChangePasswordRequest, type ConfirmForgotPasswordRequest, type ConfirmForgotPasswordResponse, type DateFilter, type DeleteUserResponse, type DisableUserResponse, type EnableUserResponse, EventEmitter, FetchAdapter, type ForceChangePasswordResponse, type ForgotPasswordRequest, type ForgotPasswordResponse, type GetChallengeDataRequest, type GetChallengeDataResponse, type GetSetupDataRequest, type GetSetupDataResponse, type GetUserSessionsResponse, type GetUsersRequest, type GetUsersResponse, type HttpAdapter, type HttpRequest, type HttpResponse, InMemoryStorage, type LinkedAccountsResponse, type LoginRequest, type LogoutAllRequest, type LogoutRequest, type MFAChallengeMethod, type MFACodeResponse, type MFADevice, type MFADeviceMethod, type MFAMethod, type MFAPasskeyResponse, type MFASetupData, type MFASetupResponse, type MFAStatus, type MfaRoutesConfig, type NAuthAdminEndpoints, NAuthClient, type NAuthClientConfig, NAuthClientError, type NAuthEndpoints, type NAuthError, NAuthErrorCode, type NAuthRedirectsConfig, type NAuthStorageAdapter, type OAuthCallbackEvent, type OAuthCompletedEvent, type OAuthErrorEvent, type OAuthStartedEvent, type RecaptchaConfig, type RecaptchaVersion, type ResendCodeRequest, type ResetPasswordWithCodeRequest, type ResetPasswordWithCodeResponse, type ResolvedNAuthClientConfig, type SignupRequest, type SocialLoginOptions, type SocialProvider, type SocialVerifyRequest, type TokenDeliveryMode, type TokenResponse, type UpdateProfileRequest, type UserSessionInfo, type VerifyEmailResponse, type VerifyPhoneCodeResponse, type VerifyPhoneCollectResponse, defaultAdminEndpoints, defaultEndpoints, getChallengeInstructions, getMFAMethod, getMaskedDestination, isOTPChallenge, requiresPhoneCollection, resolveConfig };
3338
+ export { type AdminAuditHistoryRequest, AdminOperations, type AdminResetPasswordRequest, type AdminResetPasswordResponse, type AdminSignupRequest, type AdminSignupResponse, type AdminSignupSocialRequest, type AdminSignupSocialResponse, type AuditHistoryResponse, type AuthAuditEvent, type AuthAuditEventStatus, AuthAuditEventType, AuthChallenge, type AuthChallengeEvent, type AuthErrorEvent, type AuthEvent, type AuthEventListener, type AuthEventType, type AuthLoginEvent, type AuthLogoutEvent, type AuthRefreshEvent, type AuthResponse, type AuthResponseContext, type AuthSessionExpiredEvent, type AuthSignupEvent, type AuthSuccessEvent, type AuthUser, type AuthUserSummary, type BackupCodesResponse, type BaseChallengeResponse, BrowserStorage, type ChallengeResponse, ChallengeRouter, type ChangePasswordRequest, type ConfirmForgotPasswordRequest, type ConfirmForgotPasswordResponse, type DateFilter, type DeleteUserResponse, type DisableUserResponse, type EnableUserResponse, EventEmitter, FetchAdapter, type ForceChangePasswordResponse, type ForgotPasswordRequest, type ForgotPasswordResponse, type GetChallengeDataRequest, type GetChallengeDataResponse, type GetSetupDataRequest, type GetSetupDataResponse, type GetUserSessionsResponse, type GetUsersRequest, type GetUsersResponse, type HttpAdapter, type HttpRequest, type HttpResponse, InMemoryStorage, type LinkedAccountsResponse, type LoginRequest, type LogoutAllRequest, type LogoutRequest, type MFAChallengeMethod, type MFACodeResponse, type MFADevice, type MFADeviceMethod, type MFAMethod, type MFAPasskeyResponse, type MFASetupData, type MFASetupResponse, type MFAStatus, type MfaRoutesConfig, type NAuthAdminEndpoints, NAuthClient, type NAuthClientConfig, NAuthClientError, type NAuthEndpoints, type NAuthError, NAuthErrorCode, type NAuthRedirectsConfig, type NAuthStorageAdapter, type OAuthCallbackEvent, type OAuthCompletedEvent, type OAuthErrorEvent, type OAuthStartedEvent, type RecaptchaConfig, type RecaptchaVersion, type ResendCodeRequest, type ResetPasswordWithCodeRequest, type ResetPasswordWithCodeResponse, type ResolvedNAuthClientConfig, type SignupRequest, type SocialLoginOptions, type SocialProvider, type SocialVerifyRequest, type TokenDeliveryMode, type TokenResponse, type UpdateProfileRequest, type UserSessionInfo, type VerifyEmailResponse, type VerifyPhoneCodeResponse, type VerifyPhoneCollectResponse, defaultAdminEndpoints, defaultEndpoints, getChallengeInstructions, getMFAMethod, getMaskedDestination, isOTPChallenge, requiresPhoneCollection, resolveConfig };
package/dist/index.mjs CHANGED
@@ -1577,6 +1577,18 @@ var NAuthClient = class {
1577
1577
  }
1578
1578
  /**
1579
1579
  * Refresh tokens manually.
1580
+ *
1581
+ * @throws {NAuthClientError} When refresh fails (e.g., session expired)
1582
+ *
1583
+ * @example
1584
+ * ```typescript
1585
+ * try {
1586
+ * await client.refreshTokens();
1587
+ * } catch (error) {
1588
+ * // Session expired - user is already logged out automatically
1589
+ * router.navigate(['/login']);
1590
+ * }
1591
+ * ```
1580
1592
  */
1581
1593
  async refreshTokens() {
1582
1594
  const tokenDelivery = this.getTokenDeliveryMode();
@@ -1587,10 +1599,19 @@ var NAuthClient = class {
1587
1599
  const refreshFn = async () => {
1588
1600
  return this.post(this.config.endpoints.refresh, body, false);
1589
1601
  };
1590
- const tokens = await this.tokenManager.refreshOnce(refreshFn, { persist: tokenDelivery === "json" });
1591
- this.config.onTokenRefresh?.();
1592
- this.eventEmitter.emit({ type: "auth:refresh", data: { success: true }, timestamp: Date.now() });
1593
- return tokens;
1602
+ try {
1603
+ const tokens = await this.tokenManager.refreshOnce(refreshFn, { persist: tokenDelivery === "json" });
1604
+ this.config.onTokenRefresh?.();
1605
+ this.eventEmitter.emit({ type: "auth:refresh", data: { success: true }, timestamp: Date.now() });
1606
+ return tokens;
1607
+ } catch (error) {
1608
+ if (error instanceof NAuthClientError && error.statusCode === 401) {
1609
+ await this.clearLocalAuthState();
1610
+ this.config.onSessionExpired?.();
1611
+ this.eventEmitter.emit({ type: "auth:session_expired", data: {}, timestamp: Date.now() });
1612
+ }
1613
+ throw error;
1614
+ }
1594
1615
  }
1595
1616
  // ============================================================================
1596
1617
  // Local state management (no network)