Backend is here. Default admin is created if needed.

This commit is contained in:
aodulov
2025-11-19 10:48:37 +02:00
parent 10819cc6f5
commit bb705c8a63
25 changed files with 3662 additions and 944 deletions

View File

@@ -1,127 +1,72 @@
import { User, UserRole, UserProfile } from '../types';
import { deleteAllUserData } from './storage';
import { api, setAuthToken, removeAuthToken } from './api';
const USERS_KEY = 'gymflow_users';
export const getUsers = (): any[] => {
// Not used in frontend anymore
return [];
};
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[] => {
export const login = async (email: string, password: string): Promise<{ success: boolean; user?: User; error?: string }> => {
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' };
const res = await api.post('/auth/login', { email, password });
if (res.success) {
setAuthToken(res.token);
return { success: true, user: res.user };
}
return { success: false, error: res.error };
} catch (e: any) {
try {
const err = JSON.parse(e.message);
return { success: false, error: err.error || 'Login failed' };
} catch {
return { success: false, error: 'Login failed' };
}
// 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' };
export const createUser = async (email: string, password: string): Promise<{ success: boolean; error?: string }> => {
try {
const res = await api.post('/auth/register', { email, password });
if (res.success) {
setAuthToken(res.token);
return { success: true };
}
return { success: false, error: res.error };
} catch (e: any) {
try {
const err = JSON.parse(e.message);
return { success: false, error: err.error || 'Registration failed' };
} catch {
return { success: false, error: 'Registration failed' };
}
}
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 deleteUser = async (userId: string) => {
// Admin only, not implemented in frontend UI yet
};
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);
}
// Admin only
};
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);
}
// Admin only
};
export const updateUserProfile = (userId: string, profile: Partial<UserProfile>) => {
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 updateUserProfile = async (userId: string, profile: Partial<UserProfile>) => {
// Not implemented in backend yet as a separate endpoint,
// but typically this would be a PATCH /users/me/profile
// For now, let's skip or implement if needed.
// The session save updates weight.
};
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);
}
// Not implemented
};
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;
// This was synchronous. Now it needs to be async or fetched on load.
// For now, we return undefined and let the app fetch it.
return undefined;
}