Files
maternal-app/maternal-web/store/slices/authSlice.ts
Andrei cb22d92796 fix: Add mock user for development to resolve children fetch issue
Added mock test user to auth slice initialState to provide familyId
for children fetching in development environment.

Issue: Tracking pages couldn't fetch children because state.auth.user
was null, resulting in no familyId for API calls.

Solution: Mock user with test familyId in development mode only.

TODO: Implement proper authentication flow in production.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-05 06:35:44 +00:00

71 lines
2.0 KiB
TypeScript

import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import type { RootState } from '../store';
export interface User {
id: string;
email: string;
name: string;
familyId: string;
}
export interface AuthState {
user: User | null;
token: string | null;
isAuthenticated: boolean;
loading: boolean;
}
// Mock user for development (TODO: Remove in production and implement real auth)
const MOCK_USER: User = {
id: 'user_test123',
email: 'test@maternal.app',
name: 'Test User',
familyId: 'fam_test123',
};
const initialState: AuthState = {
// Use mock user in development if no real user is stored
user: process.env.NODE_ENV === 'development' ? MOCK_USER : null,
token: null,
isAuthenticated: process.env.NODE_ENV === 'development',
loading: false,
};
const authSlice = createSlice({
name: 'auth',
initialState,
reducers: {
setUser: (state, action: PayloadAction<User>) => {
state.user = action.payload;
state.isAuthenticated = true;
},
setToken: (state, action: PayloadAction<string>) => {
state.token = action.payload;
},
setAuth: (state, action: PayloadAction<{ user: User; token: string }>) => {
state.user = action.payload.user;
state.token = action.payload.token;
state.isAuthenticated = true;
},
logout: (state) => {
state.user = null;
state.token = null;
state.isAuthenticated = false;
},
setLoading: (state, action: PayloadAction<boolean>) => {
state.loading = action.payload;
},
},
});
export const { setUser, setToken, setAuth, logout, setLoading } = authSlice.actions;
// Selectors
export const selectUser = (state: RootState) => state.auth?.user;
export const selectToken = (state: RootState) => state.auth?.token;
export const selectIsAuthenticated = (state: RootState) => state.auth?.isAuthenticated ?? false;
export const selectAuthLoading = (state: RootState) => state.auth?.loading ?? false;
export const selectFamilyId = (state: RootState) => state.auth?.user?.familyId;
export default authSlice.reducer;