feat: hackathon
This commit is contained in:
@@ -1,83 +1,72 @@
|
||||
import { useMutation, UseMutationResult } from '@tanstack/react-query';
|
||||
import { postLogin, postRegister, postSendOtp, postVerifyEmail, getGoogleAuthUrl, postGoogleCallback } from '../../api/auth';
|
||||
import {
|
||||
TLoginRequest,
|
||||
TLoginResponse,
|
||||
TRegisterRequest,
|
||||
TSendOTPRequest,
|
||||
TVerifyEmailRequest,
|
||||
TGoogleCallbackResponse,
|
||||
} from '../../types/auth';
|
||||
import { supabase } from '../../supabase';
|
||||
|
||||
import { TResponseError, TResponseMessage } from '../../types/common';
|
||||
// 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`,
|
||||
},
|
||||
});
|
||||
|
||||
export const usePostLogin = (): UseMutationResult<
|
||||
TLoginResponse,
|
||||
TResponseError,
|
||||
TLoginRequest,
|
||||
unknown
|
||||
> => {
|
||||
return useMutation({
|
||||
mutationKey: ['post-login'],
|
||||
mutationFn: async (payload) => await postLogin(payload),
|
||||
});
|
||||
};
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
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),
|
||||
});
|
||||
};
|
||||
|
||||
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 the OAuth URL for debugging
|
||||
return data;
|
||||
};
|
||||
|
||||
return {
|
||||
redirectToGoogle,
|
||||
signInWithGitHub,
|
||||
};
|
||||
};
|
||||
|
||||
export const useGoogleCallback = (): UseMutationResult<
|
||||
TGoogleCallbackResponse,
|
||||
TResponseError,
|
||||
{ code: string; state: string },
|
||||
unknown
|
||||
> => {
|
||||
return useMutation({
|
||||
mutationKey: ['google-callback'],
|
||||
mutationFn: async ({ code, state }) => await postGoogleCallback(code, state),
|
||||
});
|
||||
};
|
||||
// Supabase Email/Password authentication hook
|
||||
export const useEmailAuth = () => {
|
||||
const signInWithEmail = async (email: string, password: string) => {
|
||||
const { data, error } = await supabase.auth.signInWithPassword({
|
||||
email,
|
||||
password,
|
||||
});
|
||||
|
||||
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,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user