Files
gymflow/services/auth.ts

107 lines
3.2 KiB
TypeScript

import { User, UserRole, UserProfile } from '../types';
import { api, setAuthToken, removeAuthToken } from './api';
export const getUsers = async (): Promise<{ success: boolean; users?: User[]; error?: string }> => {
try {
const res = await api.get('/auth/users');
return res;
} catch (e) {
return { success: false, error: 'Failed to fetch users' };
}
};
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) => {
try {
const res = await api.delete(`/auth/users/${userId}`);
return res;
} catch (e) {
return { success: false, error: 'Failed to delete user' };
}
};
export const toggleBlockUser = async (userId: string, block: boolean) => {
try {
const res = await api.patch(`/auth/users/${userId}/block`, { block });
return res;
} catch (e) {
return { success: false, error: 'Failed to update user status' };
}
};
export const adminResetPassword = (userId: string, newPass: string) => {
// Admin only
};
export const updateUserProfile = async (userId: string, profile: Partial<UserProfile>): Promise<{ success: boolean; error?: string }> => {
try {
const res = await api.patch('/auth/profile', { userId, profile });
return res;
} catch (e) {
return { success: false, error: 'Failed to update profile' };
}
};
export const changePassword = async (userId: string, newPassword: string) => {
try {
const res = await api.post('/auth/change-password', { userId, newPassword });
if (!res.success) {
console.error('Failed to change password:', res.error);
}
return res;
} catch (e) {
console.error(e);
return { success: false, error: 'Network error' };
}
};
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;
};
export const getMe = async (): Promise<{ success: boolean; user?: User; error?: string }> => {
try {
const res = await api.get('/auth/me');
return res;
} catch (e) {
return { success: false, error: 'Failed to fetch user' };
}
};