Simplify signUpUtils.ts and signInUtils.ts by removing unnecessary code related to CORS proxies, optimizing for Supabase Cloud environment.
88 lines
3.3 KiB
TypeScript
88 lines
3.3 KiB
TypeScript
|
|
import { supabase } from '@/lib/supabase';
|
|
import { parseResponse, showAuthToast } from '@/utils/auth';
|
|
|
|
/**
|
|
* 회원가입 기능 - Supabase Cloud 환경에 최적화
|
|
*/
|
|
export const signUpWithDirectApi = async (email: string, password: string, username: string, redirectUrl?: string) => {
|
|
try {
|
|
console.log('Supabase Cloud 회원가입 시도 중');
|
|
|
|
// 리디렉션 URL 설정 (전달되지 않은 경우 기본값 사용)
|
|
const finalRedirectUrl = redirectUrl || `${window.location.origin}/login?auth_callback=true`;
|
|
console.log('이메일 인증 리디렉션 URL:', finalRedirectUrl);
|
|
|
|
// Supabase Cloud API를 통한 회원가입 요청
|
|
const { data, error } = await supabase.auth.signUp({
|
|
email,
|
|
password,
|
|
options: {
|
|
data: {
|
|
username // 사용자 이름을 메타데이터에 저장
|
|
},
|
|
emailRedirectTo: finalRedirectUrl
|
|
}
|
|
});
|
|
|
|
// 오류 처리
|
|
if (error) {
|
|
console.error('회원가입 오류:', error);
|
|
|
|
let errorMessage = error.message;
|
|
if (error.message.includes('User already registered')) {
|
|
errorMessage = '이미 등록된 사용자입니다.';
|
|
} else if (error.message.includes('Signup not allowed')) {
|
|
errorMessage = '회원가입이 허용되지 않습니다.';
|
|
} else if (error.message.includes('Email link invalid')) {
|
|
errorMessage = '이메일 링크가 유효하지 않습니다.';
|
|
}
|
|
|
|
showAuthToast('회원가입 실패', errorMessage, 'destructive');
|
|
return { error: { message: errorMessage }, user: null };
|
|
}
|
|
|
|
// 회원가입 성공
|
|
if (data && data.user) {
|
|
// 이메일 확인이 필요한지 확인
|
|
const isEmailConfirmationRequired = data.user.identities &&
|
|
data.user.identities.length > 0 &&
|
|
!data.user.identities[0].identity_data?.email_verified;
|
|
|
|
if (isEmailConfirmationRequired) {
|
|
// 인증 메일 전송 성공 메시지와 이메일 확인 안내
|
|
showAuthToast('회원가입 성공', '인증 메일이 발송되었습니다. 스팸 폴더도 확인해주세요.', 'default');
|
|
console.log('인증 메일 발송됨:', email);
|
|
|
|
return {
|
|
error: null,
|
|
user: data.user,
|
|
message: '이메일 인증 필요',
|
|
emailConfirmationRequired: true
|
|
};
|
|
} else {
|
|
showAuthToast('회원가입 성공', '환영합니다!', 'default');
|
|
return { error: null, user: data.user };
|
|
}
|
|
}
|
|
|
|
// 사용자 데이터가 없는 경우 (드물게 발생)
|
|
console.warn('회원가입 응답은 성공했지만 사용자 데이터가 없습니다');
|
|
showAuthToast('회원가입 성공', '계정이 생성되었습니다. 이메일 인증을 완료한 후 로그인해주세요.', 'default');
|
|
|
|
return {
|
|
error: null,
|
|
user: { email },
|
|
message: '회원가입 완료',
|
|
emailConfirmationRequired: true
|
|
};
|
|
} catch (error: any) {
|
|
console.error('회원가입 중 예외 발생:', error);
|
|
|
|
const errorMessage = error.message || '알 수 없는 오류가 발생했습니다.';
|
|
showAuthToast('회원가입 오류', errorMessage, 'destructive');
|
|
|
|
return { error: { message: errorMessage }, user: null };
|
|
}
|
|
};
|