Skip to main content

Module auth

Module auth 

Source
Expand description

Authentication state tracking for WebSocket clients.

This module provides a robust authentication tracker that coordinates login attempts and ensures each attempt produces a fresh success or failure signal before operations resume. It follows a proven pattern used in production.

§Key Features

  • Three-state model: Unauthenticated, Authenticated, Failed via AuthState enum.
  • Oneshot signaling: Each auth attempt gets a dedicated channel for result notification.
  • Superseding logic: New authentication requests cancel pending ones.
  • Timeout handling: Configurable timeout for authentication responses.
  • Generic error mapping: Adapters can map to their specific error types.
  • Auth-gated waiting: wait_for_authenticated() blocks until auth completes or fails.

Based on production usage, the recommended pattern is:

  1. Order operations: Call wait_for_authenticated() before private operations. This waits for re-auth after reconnection instead of rejecting immediately.
  2. Reconnection flow: Authenticate BEFORE resubscribing to topics.
  3. Event propagation: Send auth failures through event channels to consumers.
  4. State lifecycle: Call invalidate() on disconnect, succeed()/fail() handle auth results.

Structs§

AuthTracker
Generic authentication state tracker for WebSocket connections.

Enums§

AuthState
Authentication state for a WebSocket session.

Type Aliases§

AuthResultReceiver
AuthResultSender