Files
qlockify-frontend-deployment/src/api/users.ts

99 lines
2.9 KiB
TypeScript

import { authFetch } from './client';
// --- Auth Endpoints ---
export const loginWithPassword = async (mobile: string, password: string) => {
const response = await authFetch('/api/users/login/', {
method: 'POST',
body: JSON.stringify({ mobile, password })
});
if (!response.ok) throw new Error('Failed to login with password');
return response.json();
};
export const sendOtp = async (mobile: string, mode: string) => {
const response = await authFetch('/api/users/otp/send/', {
method: 'POST',
body: JSON.stringify({ mobile, mode })
});
if (!response.ok) throw new Error('Failed to send OTP');
return response.json();
};
export const loginWithOtp = async (mobile: string, otp: string) => {
const response = await authFetch('/api/users/otp/login/', {
method: 'POST',
body: JSON.stringify({ mobile, otp })
});
if (!response.ok) throw new Error('Failed to login with OTP');
return response.json();
};
export const logoutUser = async (refreshToken: string) => {
const response = await authFetch('/api/users/logout/', {
method: 'POST',
body: JSON.stringify({ refresh: refreshToken })
});
if (!response.ok) throw new Error("Logout failed");
return response.json();
};
// --- Profile Endpoints ---
export const getUserProfile = async () => {
const response = await authFetch('/api/users/me/', {
method: 'GET'
});
if (!response.ok) throw new Error('Failed to fetch profile');
return response.json();
};
export const updateUserProfile = async (data: Record<string, string>) => {
const response = await authFetch('/api/users/me/', {
method: 'PATCH',
body: JSON.stringify(data)
});
if (!response.ok) throw new Error('Failed to update profile');
return response.json();
};
export const updateProfilePicture = async (file: File) => {
const formData = new FormData();
formData.append('profile_picture', file);
const response = await authFetch('/api/users/profile/picture/', {
method: 'POST',
body: formData
});
if (!response.ok) throw new Error('Failed to update profile picture');
return response.json();
};
export const removeProfilePicture = async () => {
const formData = new FormData();
formData.append('profile_picture', '');
return authFetch(`/api/users/profile/picture/`, {
method: 'POST',
body: formData,
});
};
export interface SearchedUser {
id: number | string;
first_name: string;
last_name: string;
mobile: string;
profile_picture: string | null;
}
export const searchUserByExactMobile = async (mobile: string): Promise<SearchedUser | null> => {
try {
const response = await authFetch(`/api/users/search/?mobile=${encodeURIComponent(mobile)}`);
if (!response.ok) return null; // Returns null on 404 or other errors
return await response.json();
} catch (error) {
return null;
}
};