import { User, UserRole, UserProfile } from '../types'; import { deleteAllUserData } from './storage'; const USERS_KEY = 'gymflow_users'; interface StoredUser extends User { password: string; // In a real app, this would be a hash } const ADMIN_EMAIL = process.env.ADMIN_EMAIL || 'admin@gymflow.ai'; const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD || 'admin123'; export const getUsers = (): StoredUser[] => { try { const data = localStorage.getItem(USERS_KEY); return data ? JSON.parse(data) : []; } catch { return []; } }; const saveUsers = (users: StoredUser[]) => { localStorage.setItem(USERS_KEY, JSON.stringify(users)); }; export const login = (email: string, password: string): { success: boolean; user?: User; error?: string } => { // 1. Check Admin if (email === ADMIN_EMAIL && password === ADMIN_PASSWORD) { return { success: true, user: { id: 'admin_001', email: ADMIN_EMAIL, role: 'ADMIN', isFirstLogin: false, profile: { weight: 80 } } }; } // 2. Check Users const users = getUsers(); const found = users.find(u => u.email.toLowerCase() === email.toLowerCase()); if (found && found.password === password) { if (found.isBlocked) { return { success: false, error: 'Account is blocked' }; } // Return user without password field const { password, ...userSafe } = found; return { success: true, user: userSafe }; } return { success: false, error: 'Invalid credentials' }; }; export const createUser = (email: string, password: string): { success: boolean; error?: string } => { const users = getUsers(); if (users.find(u => u.email.toLowerCase() === email.toLowerCase())) { return { success: false, error: 'User already exists' }; } const newUser: StoredUser = { id: crypto.randomUUID(), email, password, role: 'USER', isFirstLogin: true, profile: { weight: 70 } }; users.push(newUser); saveUsers(users); return { success: true }; }; export const deleteUser = (userId: string) => { let users = getUsers(); users = users.filter(u => u.id !== userId); saveUsers(users); deleteAllUserData(userId); }; export const toggleBlockUser = (userId: string, block: boolean) => { const users = getUsers(); const u = users.find(u => u.id === userId); if (u) { u.isBlocked = block; saveUsers(users); } }; export const adminResetPassword = (userId: string, newPass: string) => { const users = getUsers(); const u = users.find(u => u.id === userId); if (u) { u.password = newPass; u.isFirstLogin = true; // Force them to change it saveUsers(users); } }; export const updateUserProfile = (userId: string, profile: Partial) => { const users = getUsers(); const idx = users.findIndex(u => u.id === userId); if (idx >= 0) { users[idx].profile = { ...users[idx].profile, ...profile }; saveUsers(users); } }; export const changePassword = (userId: string, newPassword: string) => { const users = getUsers(); const idx = users.findIndex(u => u.id === userId); if (idx >= 0) { users[idx].password = newPassword; users[idx].isFirstLogin = false; saveUsers(users); } }; export const getCurrentUserProfile = (userId: string): UserProfile | undefined => { if (userId === 'admin_001') return { weight: 80 }; // Mock admin profile const users = getUsers(); return users.find(u => u.id === userId)?.profile; }