# Auth Store User Usage Audit

**Date**: 2025-01-27  
**Action**: Prerequisite audit for Action 4.1.1.5 - Remove user field from authStore  
**Status**: ✅ Complete

## Summary

This audit identifies all files that use `useAuthStore().user` or destructure `user` from `useAuthStore()` to determine migration requirements before removing the `user` field from the store.

## Files Using `useAuthStore().user`

### Components (18 files)

1. **DashboardPage.tsx** (`apps/web/src/pages/DashboardPage.tsx`)
   - **Usage**: `const { user } = useAuthStore();`
   - **Line**: 42
   - **Purpose**: Display user information on dashboard
   - **Migration**: Replace with `useUser()` hook
   - **Risk**: MEDIUM

2. **DashboardPage.tsx** (`apps/web/src/features/dashboard/pages/DashboardPage.tsx`)
   - **Usage**: `const { user } = useAuthStore();`
   - **Line**: 19
   - **Purpose**: Display user information
   - **Migration**: Replace with `useUser()` hook
   - **Risk**: MEDIUM

3. **ChatPage.tsx** (`apps/web/src/features/chat/pages/ChatPage.tsx`)
   - **Usage**: `const { user, isAuthenticated } = useAuthStore();`
   - **Line**: 15
   - **Purpose**: Get user ID for chat
   - **Migration**: Replace with `useUser()` hook
   - **Risk**: MEDIUM

4. **SettingsPage.tsx** (`apps/web/src/pages/SettingsPage.tsx`)
   - **Usage**: `const { user } = useAuthStore();`
   - **Line**: 10
   - **Purpose**: Display user settings
   - **Migration**: Replace with `useUser()` hook
   - **Risk**: MEDIUM

5. **SettingsPage.tsx** (`apps/web/src/features/settings/pages/SettingsPage.tsx`)
   - **Usage**: `const { user } = useAuthStore();`
   - **Line**: 17
   - **Purpose**: Display user settings
   - **Migration**: Replace with `useUser()` hook
   - **Risk**: MEDIUM

6. **UserProfilePage.tsx** (`apps/web/src/features/profile/pages/UserProfilePage.tsx`)
   - **Usage**: `const { user: currentUser } = useAuthStore();`
   - **Line**: 26
   - **Purpose**: Compare current user with profile user
   - **Migration**: Replace with `useUser()` hook
   - **Risk**: MEDIUM

7. **Header.tsx** (`apps/web/src/components/layout/Header.tsx`)
   - **Usage**: `const { user, logout } = useAuthStore();`
   - **Line**: 35
   - **Purpose**: Display user menu, logout functionality
   - **Migration**: Replace with `useUser()` hook, keep `logout` from store
   - **Risk**: HIGH - Critical navigation component

8. **FollowButton.tsx** (`apps/web/src/features/profile/components/FollowButton.tsx`)
   - **Usage**: `const { user } = useAuthStore();`
   - **Line**: 36
   - **Purpose**: Get current user ID for follow action
   - **Migration**: Replace with `useUser()` hook
   - **Risk**: LOW

9. **LikeButton.tsx** (`apps/web/src/features/tracks/components/LikeButton.tsx`)
   - **Usage**: `const { user } = useAuthStore();`
   - **Line**: 41
   - **Purpose**: Get current user ID for like action
   - **Migration**: Replace with `useUser()` hook
   - **Risk**: LOW

10. **PlaylistFollowButton.tsx** (`apps/web/src/features/playlists/components/PlaylistFollowButton.tsx`)
    - **Usage**: `const { user } = useAuthStore();`
    - **Line**: 41
    - **Purpose**: Get current user ID for follow action
    - **Migration**: Replace with `useUser()` hook
    - **Risk**: LOW

11. **ProfileForm.tsx** (`apps/web/src/features/user/components/ProfileForm.tsx`)
    - **Usage**: `const { user, refreshUser } = useAuthStore();`
    - **Line**: 80
    - **Purpose**: Display and update user profile
    - **Migration**: Replace with `useUser()` hook, `refreshUser` needs separate handling
    - **Risk**: HIGH - Form component, needs careful migration

12. **ChatMessages.tsx** (`apps/web/src/features/chat/components/ChatMessages.tsx`)
    - **Usage**: `const { user } = useAuthStore();`
    - **Line**: 18
    - **Purpose**: Get current user for message display
    - **Migration**: Replace with `useUser()` hook
    - **Risk**: MEDIUM

13. **ChatInterface.tsx** (`apps/web/src/features/chat/components/ChatInterface.tsx`)
    - **Usage**: `const { user } = useAuthStore();`
    - **Line**: 35
    - **Purpose**: Get current user for chat
    - **Migration**: Replace with `useUser()` hook
    - **Risk**: MEDIUM

14. **ChatMessage.tsx** (`apps/web/src/features/chat/components/ChatMessage.tsx`)
    - **Usage**: `const { user } = useAuthStore();`
    - **Line**: 21
    - **Purpose**: Check if message is from current user
    - **Migration**: Replace with `useUser()` hook
    - **Risk**: LOW

15. **ChatSidebar.tsx** (`apps/web/src/features/chat/components/ChatSidebar.tsx`)
    - **Usage**: `const { user } = useAuthStore();` (2 occurrences)
    - **Line**: 45, 284
    - **Purpose**: Get current user ID for chat operations
    - **Migration**: Replace with `useUser()` hook
    - **Risk**: MEDIUM

16. **CommentThread.tsx** (`apps/web/src/features/tracks/components/CommentThread.tsx`)
    - **Usage**: `const { user } = useAuthStore();`
    - **Line**: 55
    - **Purpose**: Get current user for comment actions
    - **Migration**: Replace with `useUser()` hook
    - **Risk**: LOW

17. **CommentSection.tsx** (`apps/web/src/features/tracks/components/CommentSection.tsx`)
    - **Usage**: `const { user } = useAuthStore();`
    - **Line**: 22
    - **Purpose**: Get current user for comment actions
    - **Migration**: Replace with `useUser()` hook
    - **Risk**: LOW

18. **PlaylistList.tsx** (`apps/web/src/features/playlists/components/PlaylistList.tsx`)
    - **Usage**: `const { user } = useAuthStore();`
    - **Line**: 53
    - **Purpose**: Get current user for playlist operations
    - **Migration**: Replace with `useUser()` hook
    - **Risk**: LOW

### Hooks (4 files)

19. **useAuth.ts** (`apps/web/src/features/auth/hooks/useAuth.ts`)
    - **Usage**: `const { user, isAuthenticated, logout: storeLogout } = useAuthStore();`
    - **Line**: 7
    - **Purpose**: Auth hook wrapper
    - **Migration**: Replace with `useUser()` hook
    - **Risk**: MEDIUM - Used by other components

20. **useChat.ts** (`apps/web/src/features/chat/hooks/useChat.ts`)
    - **Usage**: `const { user } = useAuthStore();`
    - **Line**: 15
    - **Purpose**: Get user ID for chat operations
    - **Migration**: Replace with `useUser()` hook
    - **Risk**: MEDIUM

21. **useLogin.ts** (`apps/web/src/features/auth/hooks/useLogin.ts`)
    - **Usage**: `const { user, isAuthenticated } = useAuthStore.getState();`
    - **Line**: 16
    - **Purpose**: Check auth state after login
    - **Migration**: Use `useUser()` hook or query client
    - **Risk**: MEDIUM - Login flow critical

### Utilities (2 files)

22. **stateHydration.ts** (`apps/web/src/utils/stateHydration.ts`)
    - **Usage**: `const { isAuthenticated, user, isLoading } = useAuthStore.getState();` (2 occurrences)
    - **Line**: 96, 169
    - **Purpose**: Hydrate auth state on app initialization
    - **Migration**: Remove user hydration, React Query handles this
    - **Risk**: LOW - Utility function

23. **storeSelectors.ts** (`apps/web/src/utils/storeSelectors.ts`)
    - **Usage**: Examples in documentation only
    - **Line**: 309-314
    - **Purpose**: Documentation examples
    - **Migration**: Update documentation examples
    - **Risk**: LOW - Documentation only

### Documentation (1 file)

24. **STATE_SELECTORS.md** (`apps/web/src/docs/STATE_SELECTORS.md`)
    - **Usage**: Examples showing `useAuthStore().user`
    - **Line**: 14, 181-189, 229
    - **Purpose**: Documentation examples
    - **Migration**: Update documentation examples
    - **Risk**: LOW - Documentation only

## Summary Statistics

- **Components using user**: 18 files
- **Hooks using user**: 4 files
- **Utilities using user**: 2 files
- **Documentation**: 2 files
- **Total files to migrate**: 24 files (excluding tests and documentation)
- **Risk level**: MEDIUM-HIGH (critical components: Header, ProfileForm, useAuth)

## Migration Strategy

### Phase 1: Update Hooks
1. Update `useAuth.ts` to use `useUser()` hook
2. Update `useChat.ts` to use `useUser()` hook
3. Update `useLogin.ts` to use `useUser()` hook or query client

### Phase 2: Update Components
1. Update Header.tsx (HIGH priority - navigation)
2. Update ProfileForm.tsx (HIGH priority - form)
3. Update DashboardPage components
4. Update Chat components
5. Update Settings pages
6. Update other components

### Phase 3: Update Utilities
1. Update stateHydration.ts to skip user hydration
2. Update storeSelectors.ts documentation

### Phase 4: Remove User Field from Store
1. Remove `user` field from `LibraryState`
2. Remove `refreshUser` action (or migrate to React Query)
3. Update store type

## Next Steps

1. ✅ Create React Query hook (Action 4.1.1.1) - Complete
2. Replace all `useAuthStore().user` with `useUser()` (Action 4.1.1.3)
3. Remove `user` field from authStore (Action 4.1.1.5)
