import type { Socket } from 'socket.io-client'; /** * Handshake rejection codes sent by corva-subscriptions (see its README, "Handshake rejections"). */ export type UnauthorizedErrorCode = 'credentials_required' | 'credentials_bad_format' | 'token_expired' | 'invalid_token'; type ConnectError = Error & { data?: { type?: string; code?: UnauthorizedErrorCode | string; message?: string; }; }; export declare const MAX_AUTH_RECONNECT_ATTEMPTS = 3; export declare const AUTH_RECONNECT_BASE_DELAY_MS = 1000; export declare const isUnauthorizedConnectError: (err: unknown) => err is ConnectError; export interface AuthRecoveryDeps { tryRefresh: () => Promise; handleAuthFailure: (error: unknown) => void; isLoginRoute: () => boolean; /** * Impersonation swaps only the auth cookie; the refresh cookie stays the admin's. A refresh would * therefore come back as a plain admin session and silently end the impersonation, so while * impersonating the socket is left down and the existing HTTP 401 handling decides what happens. */ isImpersonating: () => boolean; } export interface SocketAuthRecovery { /** * Reconnect a socket that has given up: socket.io stops reconnecting for good after a handshake * rejection, and so do we once the attempt cap is reached. No-op while connected or while either * side is still retrying. Meant for "the tab is visible again" moments. */ ensureConnected(): void; /** Detach the listeners and cancel any pending retry. */ dispose(): void; } /** * Recover from an authentication rejection at the socket.io handshake. * * The server verifies the session cookie once, during the handshake. When the JWT in it has * expired (typically after the tab was suspended long enough for the connection to drop), the * handshake is rejected and socket.io-client treats that as final: it destroys the socket and sets * `skipReconnect`, so nothing ever retries. HTTP calls recover through `AuthRecovery` on their next * 401, but the socket never issues an HTTP call, so it stayed dead until a page reload. * * This closes the loop: on an `UnauthorizedError` connect_error, refresh the session (the browser * then re-sends the fresh cookie on the next handshake) and call `socket.connect()`, with capped * exponential backoff. A failed refresh is handed to `AuthRecovery.handleAuthFailure`, which * redirects to /login when the session is really gone and does nothing for a transient failure. */ export declare const attachAuthRecovery: (socket: Socket, deps?: Partial) => SocketAuthRecovery; export {}; //# sourceMappingURL=subscriptions-auth-recovery.d.ts.map