MIF_E31221222/sigap-website/app/(pages)/(auth)/mutation.ts

53 lines
1.6 KiB
TypeScript

import { useMutation } from '@tanstack/react-query';
import { signIn, signOut, verifyOtp } from './action';
export function useAuthActions() {
// Sign In Mutation
const signInMutation = useMutation({
mutationFn: async (formData: FormData) => {
const email = formData.get("email")?.toString()
const response = await signIn(formData);
// If the server action returns an error, treat it as an error for React Query
if (response?.error) {
throw new Error(response.error);
}
return { email };
}
});
const verifyOtpMutation = useMutation({
mutationFn: async (formData: FormData) => {
const email = formData.get("email")?.toString()
const token = formData.get("token")?.toString()
const response = await verifyOtp(formData);
// If the server action returns an error, treat it as an error for React Query
if (response?.error) {
throw new Error(response.error);
}
return { email, token };
}
})
const signOutMutation = useMutation({
mutationFn: async () => {
const response = await signOut();
// If the server action returns an error, treat it as an error for React Query
if (response?.error) {
throw new Error(response.error);
}
return response;
}
})
return {
signIn: signInMutation,
verifyOtp: verifyOtpMutation,
signOut: signOutMutation
};
}