Files
maternal-app/maternal-web/store/slices/authSlice.ts
Andrei 52144ca4a9 fix: Add mock children data to Redux store for development
Added Alice and Bob as mock children in development mode to allow
tracking pages and UI to work without requiring authentication.

Changes:
- Updated childrenSlice to use childrenApi for consistent backend calls
- Pre-populated Redux store with 2 mock children (Alice, Bob)
- Set selectedChildId to first child by default
- Added mock token to localStorage for API client

This allows all tracking forms and ChildSelector components to work
in development without needing real login/auth flow.

TODO: Remove mocks and implement real authentication in production.

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

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

79 lines
2.5 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 and token 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',
};
// Mock JWT token for development - this is just for testing, real auth needed in production
const MOCK_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJ1c2VyX3Rlc3QxMjMiLCJmYW1pbHlJZCI6ImZhbV90ZXN0MTIzIiwiaWF0IjoxNjAwMDAwMDAwfQ.mock_signature_for_development';
// Set mock token in localStorage for development
if (typeof window !== 'undefined' && process.env.NODE_ENV === 'development') {
localStorage.setItem('accessToken', MOCK_TOKEN);
}
const initialState: AuthState = {
// Use mock user in development if no real user is stored
user: process.env.NODE_ENV === 'development' ? MOCK_USER : null,
token: process.env.NODE_ENV === 'development' ? MOCK_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;