2025-11-25 01:47:56 +07:00
|
|
|
import { supabase } from '../../supabase';
|
2025-11-25 09:49:51 +07:00
|
|
|
import { useMutation } from '@tanstack/react-query';
|
|
|
|
|
import * as authApi from '../../api/auth';
|
2025-04-03 12:21:10 +07:00
|
|
|
|
2025-11-25 02:34:52 +07:00
|
|
|
export * from './use-auth-store';
|
|
|
|
|
|
2025-11-25 09:49:51 +07:00
|
|
|
// React Query hooks for auth API
|
|
|
|
|
export const usePostLogin = () => {
|
|
|
|
|
return useMutation({
|
|
|
|
|
mutationFn: authApi.postLogin,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const usePostRegister = () => {
|
|
|
|
|
return useMutation({
|
|
|
|
|
mutationFn: authApi.postRegister,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const usePostVerifyEmail = () => {
|
|
|
|
|
return useMutation({
|
|
|
|
|
mutationFn: authApi.postVerifyEmail,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const usePostSendOtp = () => {
|
|
|
|
|
return useMutation({
|
|
|
|
|
mutationFn: authApi.postSendOtp,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useGoogleCallback = () => {
|
|
|
|
|
return useMutation({
|
|
|
|
|
mutationFn: ({ code, state }: { code: string; state: string }) =>
|
|
|
|
|
authApi.postGoogleCallback(code, state),
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-25 01:47:56 +07:00
|
|
|
// Supabase GitHub OAuth hook
|
|
|
|
|
export const useGitHubAuth = () => {
|
|
|
|
|
const signInWithGitHub = async () => {
|
|
|
|
|
const { data, error } = await supabase.auth.signInWithOAuth({
|
|
|
|
|
provider: 'github',
|
|
|
|
|
options: {
|
|
|
|
|
redirectTo: `${globalThis.location.origin}/auth/callback`,
|
|
|
|
|
},
|
|
|
|
|
});
|
2025-03-19 23:38:42 +07:00
|
|
|
|
2025-11-25 01:47:56 +07:00
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2025-08-18 12:50:32 +07:00
|
|
|
|
2025-11-25 01:47:56 +07:00
|
|
|
// Return the OAuth URL for debugging
|
|
|
|
|
return data;
|
2025-08-18 12:50:32 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
2025-11-25 01:47:56 +07:00
|
|
|
signInWithGitHub,
|
2025-08-18 12:50:32 +07:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-25 01:47:56 +07:00
|
|
|
// Supabase Email/Password authentication hook
|
|
|
|
|
export const useEmailAuth = () => {
|
|
|
|
|
const signInWithEmail = async (email: string, password: string) => {
|
|
|
|
|
const { data, error } = await supabase.auth.signInWithPassword({
|
|
|
|
|
email,
|
|
|
|
|
password,
|
|
|
|
|
});
|
2025-08-18 12:50:32 +07:00
|
|
|
|
2025-11-25 01:47:56 +07:00
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const signUpWithEmail = async (email: string, password: string, fullname: string) => {
|
|
|
|
|
const { data, error } = await supabase.auth.signUp({
|
|
|
|
|
email,
|
|
|
|
|
password,
|
|
|
|
|
options: {
|
|
|
|
|
data: {
|
|
|
|
|
full_name: fullname,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const signOut = async () => {
|
|
|
|
|
const { error } = await supabase.auth.signOut();
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
signInWithEmail,
|
|
|
|
|
signUpWithEmail,
|
|
|
|
|
signOut,
|
|
|
|
|
};
|
|
|
|
|
};
|