sdk-simple-auth
Version:
Universal JavaScript/TypeScript authentication SDK with multi-backend support, automatic token refresh, and React integration
173 lines • 4.64 kB
TypeScript
import { AuthCallbacks, AuthConfig, AuthState, AuthTokens, AuthUser, ExtendedSessionInfo, LoginCredentials, RegisterData } from '../types';
type StateChangeListener = (state: AuthState) => void;
/**
* Refactored AuthSDK with improved modularity and session management
*/
export declare class AuthSDK {
private config;
private state;
private callbacks;
private storageManager;
private refreshManager;
private sessionValidator;
private axiosInterceptorManager;
private expirationTimer;
private stateChangeListeners;
private isInitialized;
constructor(config: AuthConfig, callbacks?: AuthCallbacks);
/**
* Build complete configuration with defaults
*/
private buildConfig;
/**
* Enhanced login with automatic session establishment
*/
login(credentials: LoginCredentials): Promise<AuthUser>;
/**
* Enhanced register with automatic session establishment when tokens are provided
*/
register(userData: RegisterData): Promise<AuthUser>;
/**
* Enhanced logout with complete session cleanup
*/
logout(): Promise<void>;
/**
* Clear local session without calling backend
* Useful when the server has already invalidated the session (401/422)
*/
clearLocalSession(): Promise<void>;
/**
* Enhanced token refresh with session renewal
*/
refreshTokens(): Promise<AuthTokens>;
/**
* Subscribe to authentication state changes
*/
onAuthStateChanged(listener: StateChangeListener): () => void;
/**
* Get current authentication state
*/
getState(): AuthState;
/**
* Get current authenticated user
*/
getCurrentUser(): AuthUser | null;
/**
* Get current access token
*/
getAccessToken(): string | null;
/**
* Get current refresh token
*/
getRefreshToken(): string | null;
/**
* Check if user is currently authenticated
*/
isAuthenticated(): Promise<boolean>;
/**
* Validate current session with the server using refresh token
* This is called automatically when the app regains focus/visibility
*/
validateSession(): Promise<boolean>;
/**
* Get a valid access token, refreshing if necessary
*/
getValidAccessToken(): Promise<string | null>;
/**
* Get authorization headers for API requests
*/
getAuthHeaders(): Promise<Record<string, string>>;
/**
* Debug token information
*/
debugToken(token?: string): void;
/**
* Debug API response structure
*/
debugResponse(response: any): void;
/**
* Force refresh tokens regardless of expiration
*/
forceRefreshTokens(): Promise<AuthTokens>;
/**
* Get comprehensive session information
*/
getExtendedSessionInfo(): Promise<ExtendedSessionInfo>;
/**
* Get detailed session information (legacy method)
*/
getSessionInfo(): Promise<{
isValid: boolean;
user: AuthUser | null;
tokenType: string | null;
expiresIn: number | null;
refreshAvailable: boolean;
sessionId: string | null;
}>;
/**
* Test extraction with mock response (debugging)
*/
testExtraction(response: any): void;
/**
* Detect token format
*/
private detectTokenFormat;
/**
* Initialize from stored authentication data
*/
private initializeFromStorage;
/**
* Establish new session with tokens and user
*/
private establishSession;
/**
* Clear current session completely
*/
private clearSession;
/**
* Handle token refresh completion
*/
private handleTokenRefresh;
/**
* Schedule automatic token expiration handling
*/
private scheduleTokenExpiration;
/**
* Handle automatic token expiration
*/
private handleTokenExpiration;
/**
* Clear expiration timer
*/
private clearExpirationTimer;
/**
* Validate token based on its type
*/
private isTokenValid;
/**
* Validate stored token using metadata
*/
private validateStoredToken;
/**
* Create default HTTP client using fetch
*/
private createDefaultHttpClient;
/**
* Set loading state
*/
private setLoading;
/**
* Set error state
*/
private setError;
/**
* Notify all state change listeners
*/
private notifyStateChange;
/**
* NUEVO: Debug current session with comprehensive info
*/
debugSession(): void;
}
export {};
//# sourceMappingURL=AuthSDK.d.ts.map