Skip to content

[FE-03] Set up Zustand auth store — user, token, isAuthenticated, login, logout actions #815

Description

@mftee

Overview

The application needs a global auth state store to share the current user and access token across components without prop drilling. Zustand is already installed. Without this store, there is no centralized place for the API interceptor (FE-01) to read the token from, and every protected component must independently check auth state.

Background

File to create: frontend/lib/store/auth.store.ts

Store shape:

interface AuthState {
  user: UserDto | null;
  accessToken: string | null;
  isAuthenticated: boolean;
  login: (user: UserDto, accessToken: string) => void;
  logout: () => void;
  setToken: (token: string) => void;
}

Use Zustand's persist middleware to hydrate from localStorage on page refresh. Persist the user object and token. The API interceptor (FE-01) should read useAuthStore.getState().accessToken to avoid hook-call-outside-component errors.

Also create a useCurrentUser() helper hook that fetches the authenticated user from GET /api/users/me on mount and updates the store.

Acceptance Criteria

  • Zustand store created with all fields and actions listed above
  • State persisted to localStorage via persist middleware
  • logout() clears state and removes from localStorage
  • useCurrentUser() hook calls /api/users/me and populates the store on mount
  • TypeScript types for UserDto match the backend response

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions