feat: implement API composables for authentication and data fetching
- Add useApiFetch composable for HTTP requests with auth token - Implement useAuth composable for authentication endpoints - Add useAdmin composable for admin operations - Add useOperator composable for operator management - Add useMembers composable for member data - Add usePublic composable for public reports - Add useReports composable for report operations Co-authored-by: Copilot <[email protected]>
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import type {
|
||||
ChangePasswordRequest,
|
||||
CurrentUserResponse, EmailRequest, NotificationListResponse,
|
||||
OkResponse,
|
||||
OtpRequestResponse, PasswordLoginRequest, RegisterCitizenRequest,
|
||||
RegisterCitizenResponse, RewardsResponse,
|
||||
TokenResponse, VerifyOtpRequest
|
||||
} from "~/types/api";
|
||||
|
||||
export const useAuth = () => {
|
||||
const register = (body: RegisterCitizenRequest) =>
|
||||
useApiFetch<RegisterCitizenResponse>('/api/v1/auth/register', { method: 'POST', body })
|
||||
|
||||
const requestOtp = (body: EmailRequest) =>
|
||||
useApiFetch<OtpRequestResponse>('/api/v1/auth/otp/request', { method: 'POST', body })
|
||||
|
||||
const verifyOtp = (body: VerifyOtpRequest) =>
|
||||
useApiFetch<TokenResponse>('/api/v1/auth/otp/verify', { method: 'POST', body })
|
||||
|
||||
const loginWithPassword = (body: PasswordLoginRequest) =>
|
||||
useApiFetch<TokenResponse>('/api/v1/auth/login/password', { method: 'POST', body })
|
||||
|
||||
const changePassword = (body: ChangePasswordRequest) =>
|
||||
useApiFetch<OkResponse>('/api/v1/auth/change-password', { method: 'POST', body })
|
||||
|
||||
const fetchCurrentUser = () =>
|
||||
useApiFetch<CurrentUserResponse>('/api/v1/me', { method: 'GET' })
|
||||
|
||||
const fetchRewards = (query?: { page?: number; per_page?: number }) =>
|
||||
useApiFetch<RewardsResponse>('/api/v1/me/rewards', { method: 'GET', query })
|
||||
|
||||
const fetchNotifications = (query?: { page?: number; per_page?: number }) =>
|
||||
useApiFetch<NotificationListResponse>('/api/v1/notifications', { method: 'GET', query })
|
||||
|
||||
const markNotificationRead = (id: number) =>
|
||||
useApiFetch<OkResponse>(`/api/v1/notifications/${id}/read`, { method: 'PATCH' })
|
||||
|
||||
return {
|
||||
register,
|
||||
requestOtp,
|
||||
verifyOtp,
|
||||
loginWithPassword,
|
||||
changePassword,
|
||||
fetchCurrentUser,
|
||||
fetchRewards,
|
||||
fetchNotifications,
|
||||
markNotificationRead,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user