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.
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 throughtokenStore(so it survives reloads) and the current user is re-fetched fromauthConfig.mePathwhenever the token changes. Aauth:unauthorizedwindow event (dispatched bycreateApiClienton any401) also clears the session.Notes:
useAuth.loginstoresaccess_tokenfrom the response and setscurrentUserfrom the responseuser; failures seterrorfromdetailor a default message.registercreates the account then immediately logs in.logoutis best-effort on the server and always clears local state.Props:
children- the subtree that needs auth context.authConfig- endpoint map; seeAuthServerConfig.onUnauthorized- optional callback invoked when the session is cleared.