Function AuthProvider

  • Provide authentication state to the React tree.

    Holds the current user, token, loading and error state, and exposes login / register / logout. The token is persisted through tokenStore (so it survives reloads) and the current user is re-fetched from authConfig.mePath whenever the token changes. A auth:unauthorized window event (dispatched by createApiClient on any 401) also clears the session.

    Notes:

    • Must wrap any component that calls useAuth.
    • login stores access_token from the response and sets currentUser from the response user; failures set error from detail or a default message.
    • register creates the account then immediately logs in.
    • logout is best-effort on the server and always clears local state.

    Props:

    • children - the subtree that needs auth context.
    • authConfig - endpoint map; see AuthServerConfig.
    • onUnauthorized - optional callback invoked when the session is cleared.

    Parameters

    • __namedParameters: {
          authConfig: AuthServerConfig;
          children: ReactNode;
          onUnauthorized?: () => void;
      }

    Returns Element

    <AuthProvider authConfig={authConfig}>
    <App />
    </AuthProvider>