import api from './client';
import type { User, LoginResponse, SignupData } from '../types';
export const authApi = {
getCurrentUser: async (): Promise<User | null> => {
try {
const { data } = await api.get<{ user: User | null }>('/api/auth/user');
return data.user;
} catch {
return null;
}
},
login: async (email: string, password: string): Promise<LoginResponse> => {
const { data } = await api.post<LoginResponse>('/api/auth/login', {
email,
password,
});
return data;
},
signup: async (data: SignupData): Promise<LoginResponse> => {
const { data: result } = await api.post<LoginResponse>('/api/auth/signup', data);
return result;
},
logout: async (): Promise<void> => {
await api.post('/api/auth/logout');
},
updateProfile: async (full_name: string): Promise<User> => {
const { data } = await api.put<{ user: { email: string; full_name: string; role: string; id: number } }>(
'/api/auth/user',
{ full_name },
);
return {
id: data.user.id,
email: data.user.email,
full_name: data.user.full_name,
role: data.user.role,
};
},
forgotPassword: async (email: string): Promise<{ success?: boolean; message?: string }> => {
const { data } = await api.post<{ success?: boolean; message?: string }>('/api/auth/forgot-password', { email });
return data;
},
resetPassword: async (token: string, password: string): Promise<{ success: boolean; message: string }> => {
const { data } = await api.post<{ success: boolean; message: string }>('/api/auth/reset-password', {
token,
password,
});
return data;
},
changePassword: async (currentPassword: string, newPassword: string): Promise<{ success: boolean; message: string }> => {
const { data } = await api.post<{ success: boolean; message: string }>('/api/auth/change', {
currentPassword,
newPassword,
});
return data;
},
};