Files
imphnen-frontend-service/libs/service/src/hooks/auth/index.ts
T

84 lines
1.9 KiB
TypeScript
Raw Normal View History

2025-03-19 23:38:42 +07:00
import { useMutation, UseMutationResult } from '@tanstack/react-query';
import { postLogin, postRegister, postSendOtp, postVerifyEmail, getGoogleAuthUrl, postGoogleCallback } from '../../api/auth';
2025-03-19 23:38:42 +07:00
import {
TLoginRequest,
TLoginResponse,
TRegisterRequest,
2025-06-18 18:50:06 +07:00
TSendOTPRequest,
2025-03-19 23:38:42 +07:00
TVerifyEmailRequest,
TGoogleCallbackResponse,
2025-03-19 23:38:42 +07:00
} from '../../types/auth';
2025-04-03 12:21:10 +07:00
2025-03-19 23:38:42 +07:00
import { TResponseError, TResponseMessage } from '../../types/common';
export const usePostLogin = (): UseMutationResult<
TLoginResponse,
TResponseError,
TLoginRequest,
unknown
> => {
return useMutation({
mutationKey: ['post-login'],
mutationFn: async (payload) => await postLogin(payload),
});
};
2025-03-19 23:38:42 +07:00
export const usePostRegister = (): UseMutationResult<
TResponseMessage,
TResponseError,
TRegisterRequest,
unknown
> => {
return useMutation({
mutationKey: ['post-register'],
mutationFn: async (payload) => await postRegister(payload),
});
};
export const usePostVerifyEmail = (): UseMutationResult<
TResponseMessage,
TResponseError,
TVerifyEmailRequest,
unknown
> => {
return useMutation({
mutationKey: ['post-verify-email'],
mutationFn: async (payload) => await postVerifyEmail(payload),
});
};
2025-06-18 18:50:06 +07:00
export const usePostSendOTP = (): UseMutationResult<
TResponseMessage,
TResponseError,
TSendOTPRequest,
unknown
> => {
return useMutation({
mutationKey: ['post-send-otp'],
mutationFn: async (payload) => await postSendOtp(payload),
});
};
export const useGoogleAuth = () => {
const redirectToGoogle = async (): Promise<string> => {
return await getGoogleAuthUrl();
};
return {
redirectToGoogle,
};
};
export const useGoogleCallback = (): UseMutationResult<
TGoogleCallbackResponse,
TResponseError,
{ code: string; state: string },
unknown
> => {
return useMutation({
mutationKey: ['google-callback'],
mutationFn: async ({ code, state }) => await postGoogleCallback(code, state),
});
};