Files
gymflow/services/auth.ts

73 lines
2.2 KiB
TypeScript

import { User, UserRole, UserProfile } from '../types';
import { api, setAuthToken, removeAuthToken } from './api';
export const getUsers = (): any[] => {
// Not used in frontend anymore
return [];
};
export const login = async (email: string, password: string): Promise<{ success: boolean; user?: User; error?: string }> => {
try {
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' };
}
}
};
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' };
}
}
};
export const deleteUser = async (userId: string) => {
// Admin only, not implemented in frontend UI yet
};
export const toggleBlockUser = (userId: string, block: boolean) => {
// Admin only
};
export const adminResetPassword = (userId: string, newPass: string) => {
// Admin only
};
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) => {
// Not implemented
};
export const getCurrentUserProfile = (userId: string): UserProfile | undefined => {
// 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;
}