UNPKG

@frank-auth/react

Version:

Flexible and customizable React UI components for Frank Authentication

159 lines 4.48 kB
import { Session, SessionInfo } from '@frank-auth/client'; import { AuthError } from '../provider/types'; export interface UseSessionReturn { session: Session | null; sessions: SessionInfo[]; isLoaded: boolean; isLoading: boolean; error: AuthError | null; createSession: (token: string) => Promise<Session>; setActiveSession: (sessionId: string) => Promise<void>; refreshSession: () => Promise<Session | null>; revokeSession: (sessionId: string) => Promise<void>; revokeAllSessions: (exceptCurrent?: boolean) => Promise<void>; endSession: () => Promise<void>; sessionId: string | null; sessionToken: string | null; expiresAt: Date | null; lastActiveAt: Date | null; isActive: boolean; isExpired: boolean; isExpiring: boolean; timeUntilExpiry: number | null; deviceInfo: DeviceInfo | null; isCurrentDevice: boolean; isTrustedDevice: boolean; hasMultipleSessions: boolean; sessionCount: number; otherSessions: SessionInfo[]; } export interface DeviceInfo { userAgent: string; browser: string; os: string; device: string; ipAddress: string; location?: { city?: string; country?: string; region?: string; }; } /** * Session management hook providing access to all session functionality * * @example Basic session management * ```tsx * import { useSession } from '@frank-auth/react'; * * function SessionManager() { * const { * session, * sessions, * revokeSession, * revokeAllSessions, * isExpiring * } = useSession(); * * if (isExpiring) { * return ( * <div className="session-warning"> * <p>Your session is about to expire</p> * <button onClick={refreshSession}>Extend Session</button> * </div> * ); * } * * return ( * <div> * <h3>Active Sessions ({sessions.length})</h3> * {sessions.map((session) => ( * <div key={session.id}> * <p>{session.deviceInfo?.browser} on {session.deviceInfo?.os}</p> * <p>Last active: {session.lastActiveAt}</p> * <button onClick={() => revokeSession(session.id)}> * Revoke Session * </button> * </div> * ))} * <button onClick={() => revokeAllSessions(true)}> * Revoke All Other Sessions * </button> * </div> * ); * } * ``` * * @example Session expiry warning * ```tsx * function SessionExpiryWarning() { * const { isExpiring, timeUntilExpiry, refreshSession } = useSession(); * * if (!isExpiring || !timeUntilExpiry) return null; * * return ( * <div className="alert alert-warning"> * <p>Session expires in {timeUntilExpiry} minutes</p> * <button onClick={refreshSession}> * Extend Session * </button> * </div> * ); * } * ``` */ export declare function useSession(): UseSessionReturn; /** * Hook for session status monitoring */ export declare function useSessionStatus(): { isActive: boolean; isExpired: boolean; isExpiring: boolean; timeUntilExpiry: number | null; expiresAt: Date | null; refreshSession: () => Promise<Session | null>; status: string; }; /** * Hook for multi-session management */ export declare function useMultiSession(): { sessions: SessionInfo[]; sessionCount: number; otherSessions: SessionInfo[]; hasMultipleSessions: boolean; revokeSession: (sessionId: string) => Promise<void>; revokeAllSessions: (exceptCurrent?: boolean) => Promise<void>; setActiveSession: (sessionId: string) => Promise<void>; isLoading: boolean; error: AuthError | null; revokeAllOthers: () => Promise<void>; }; /** * Hook for device and security information */ export declare function useSessionSecurity(): { deviceInfo: DeviceInfo | null; isCurrentDevice: boolean; isTrustedDevice: boolean; sessionId: string | null; lastActiveAt: Date | null; isSecure: boolean; }; /** * Hook that automatically handles session expiry and refresh */ export declare function useSessionExpiry(options?: { autoRefresh?: boolean; refreshThreshold?: number; onExpiry?: () => void; onExpiring?: () => void; }): { isExpired: boolean; isExpiring: boolean; timeUntilExpiry: number | null; refreshSession: () => Promise<Session | null>; autoRefresh: boolean; }; //# sourceMappingURL=use-session.d.ts.map