Overview
TheuseAuthStore is a Zustand-powered state management hook that handles authentication state, user data, and session lifecycle. It provides methods for initializing the session, logging in, and logging out.
Import
Type Signature
State Properties
Indicates whether the user is currently authenticated.
true if a valid user session exists.Indicates whether the authenticated user has completed onboarding. Mirrors the
user.is_onboarded value.The authenticated user object, or
null if not authenticated.User Properties:id: Unique user identifieremail: User’s email addressrole: Either'ORGANIZER'or'TEAM_ADMIN'is_onboarded: Whether user completed onboarding
Indicates whether the store has completed initialization. Prevents redundant API calls on mount.
Methods
init()
Asynchronously initializes the authentication state by fetching the current user from the API.- Checks if already initialized; returns early if true
- Calls
GET /auth/meto fetch current user - On success: sets user data and authentication flags
- On failure: clears user data and sets authenticated to false
- Always marks the store as initialized
login(user)
Sets the authentication state after successful login.The authenticated user object containing:
id: User identifieremail: User’s emailrole: User role ('ORGANIZER'or'TEAM_ADMIN')is_onboarded: Onboarding status
logout()
Clears the authentication state and user data.- Sets
usertonull - Sets
isAuthenticatedtofalse - Sets
isOnboardedtofalse - Keeps
initializedastrue
Usage Examples
Accessing Auth State in Components
Optimized Selectors
Conditional Rendering by Role
Outside React Components
Integration with API Client
The store is integrated with the API client atsrc/app/api/client.ts. When a request returns a 401 Unauthorized status, the client automatically calls useAuthStore.getState().logout() to clear the session.
Initial State
Source
Implemented in:src/app/store/auth/auth.store.ts:11