90 lines
2.4 KiB
TypeScript
90 lines
2.4 KiB
TypeScript
import { useMutation } from '@tanstack/react-query';
|
|
import { sendMagicLink, sendPasswordRecovery, signIn, signOut, verifyOtp } from './action';
|
|
|
|
export function useAuthActions() {
|
|
// Sign In Mutation
|
|
const signInMutation = useMutation({
|
|
mutationKey: ["signIn"],
|
|
mutationFn: async (formData: FormData) => await signIn(formData)
|
|
});
|
|
|
|
// Verify OTP Mutation
|
|
const verifyOtpMutation = useMutation({
|
|
mutationKey: ["verifyOtp"],
|
|
mutationFn: async (formData: FormData) => await verifyOtp(formData)
|
|
});
|
|
|
|
const signOutMutation = useMutation({
|
|
mutationKey: ["signOut"],
|
|
mutationFn: async () => await signOut()
|
|
});
|
|
|
|
const sendMagicLinkMutation = useMutation({
|
|
mutationKey: ["sendMagicLink"],
|
|
mutationFn: async (email: string) => await sendMagicLink(email)
|
|
});
|
|
|
|
const sendPasswordRecoveryMutation = useMutation({
|
|
mutationKey: ["sendPasswordRecovery"],
|
|
mutationFn: async (email: string) => await sendPasswordRecovery(email)
|
|
});
|
|
|
|
return {
|
|
signIn: signInMutation,
|
|
verifyOtp: verifyOtpMutation,
|
|
signOut: signOutMutation,
|
|
sendMagicLink: sendMagicLinkMutation,
|
|
sendPasswordRecovery: sendPasswordRecoveryMutation
|
|
};
|
|
}
|
|
|
|
export const useSignInMutation = () => {
|
|
const { signIn } = useAuthActions();
|
|
|
|
return {
|
|
signIn: signIn.mutateAsync,
|
|
isPending: signIn.isPending,
|
|
error: signIn.error,
|
|
};
|
|
}
|
|
|
|
export const useVerifyOtpMutation = () => {
|
|
const { verifyOtp } = useAuthActions();
|
|
|
|
return {
|
|
verifyOtp: verifyOtp.mutateAsync,
|
|
isPending: verifyOtp.isPending,
|
|
error: verifyOtp.error,
|
|
}
|
|
}
|
|
|
|
export const useSignOutMutation = () => {
|
|
const { signOut } = useAuthActions();
|
|
|
|
return {
|
|
signOut: signOut.mutateAsync,
|
|
isPending: signOut.isPending
|
|
}
|
|
}
|
|
|
|
export const useSendMagicLinkMutation = () => {
|
|
const { sendMagicLink } = useAuthActions();
|
|
|
|
return {
|
|
sendMagicLink: sendMagicLink.mutateAsync,
|
|
isPending: sendMagicLink.isPending,
|
|
error: sendMagicLink.error,
|
|
}
|
|
}
|
|
|
|
export const useSendPasswordRecoveryMutation = () => {
|
|
const { sendPasswordRecovery } = useAuthActions();
|
|
|
|
return {
|
|
sendPasswordRecovery: sendPasswordRecovery.mutateAsync,
|
|
isPending: sendPasswordRecovery.isPending,
|
|
error: sendPasswordRecovery.error,
|
|
}
|
|
}
|
|
|