useAuthStore
Stock Screening Platform - Frontend API v0.1.0
Stock Screening Platform - Frontend API / store/authStore / useAuthStore
Variable: useAuthStore
constuseAuthStore:UseBoundStore<WithPersist<StoreApi<AuthState>,AuthState>>
Defined in: src/store/authStore.ts:159
Global authentication store hook.
Provides access to authentication state and actions throughout the application. Uses Zustand for simple, performant state management with automatic persistence.
Features
- Persistent Storage: Auth state persists across page refreshes
- Token Management: Automatic token storage and retrieval
- Type Safety: Full TypeScript support
- Reactive Updates: Components re-render on auth state changes
Examples
Access auth state
const { user, isAuthenticated } = useAuthStore();
if (!isAuthenticated) {
return <Navigate to="/login" />;
}
return <div>Welcome, {user.username}!</div>;
Use auth actions
const { login, logout } = useAuthStore();
const handleLogin = async (email, password) => {
const response = await authService.login(email, password);
login(response.user, response.access_token, response.refresh_token);
};
Selective subscription (performance optimization)
// Only re-render when isAuthenticated changes
const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
Returns
Authentication store state and actions
See
- User for user profile structure
- authService for authentication API calls