Files
saas_frontend/src/application/Authentication/AuthApi.ts
T
2026-01-19 11:05:40 +05:30

77 lines
2.8 KiB
TypeScript

import { apiClient } from "../../lib/apiClient";
import type { AuthUser, SigninRequest, SignupRequest, TokenResponse } from "./AuthTypes";
type AuthApiOptions = {
tenantId?: string;
};
type LogoutResponse = {
message: string;
};
type UpdateProfilePayload = {
first_name?: string;
last_name?: string;
phone_number?: string;
};
const withTenantHeader = (tenantId?: string) =>
tenantId ? { headers: { "X-Tenant-ID": tenantId } } : undefined;
export const authApi = {
signin: (payload: SigninRequest, options?: AuthApiOptions) =>
apiClient.post<TokenResponse>(
"/api/auth/signin",
payload,
{ ...withTenantHeader(options?.tenantId), successMessage: "Signed in successfully", errorMessage: "Failed to sign in" }
),
signup: (payload: SignupRequest, options?: AuthApiOptions) =>
apiClient.post<AuthUser>(
"/api/auth/signup",
payload,
{ ...withTenantHeader(options?.tenantId), successMessage: "Account created", errorMessage: "Failed to create account" }
),
logout: () => apiClient.post<LogoutResponse>("/api/auth/logout", { successMessage: "Signed out", errorMessage: "Failed to sign out" }),
me: () => apiClient.get<AuthUser>("/api/auth/me"),
resetPassword: (oldPassword: string, newPassword: string) =>
apiClient.post<{ message: string }>("/api/auth/reset-password", {
old_password: oldPassword,
new_password: newPassword,
}, { successMessage: "Password updated", errorMessage: "Failed to update password" }),
forgotPassword: (email: string) =>
apiClient.post<{ message: string }>("/api/auth/forgot-password", { email }, {
successMessage: "If email exists, OTP sent",
errorMessage: "Failed to process request"
}),
verifyOtp: (email: string, otp: string) =>
apiClient.post<{ message: string }>("/api/auth/verify-otp", { email, otp }, {
successMessage: "OTP Verified",
errorMessage: "Invalid OTP"
}),
resetPasswordWithOtp: (email: string, otp: string, newPassword: string) =>
apiClient.post<{ message: string }>("/api/auth/reset-password-otp", {
email,
otp,
new_password: newPassword
}, { successMessage: "Password reset successfully", errorMessage: "Failed to reset password" }),
updateProfile: (userId: string, payload: UpdateProfilePayload) =>
apiClient.put<AuthUser>(`/api/auth/update/${userId}`, payload, {
successMessage: "Profile updated successfully",
errorMessage: "Failed to update profile",
}),
updateLanguage: (userId: string, language: string) =>
apiClient.patch<AuthUser>(`/api/auth/update/${userId}/language`, { preferred_language: language }, {
silent: true,
}),
refresh: (refreshToken: string) =>
apiClient.post<TokenResponse>("/api/auth/refresh", {
refresh_token: refreshToken,
}),
};