The signUp function in AuthContextType was expecting only two arguments (email, password) but the RegisterForm component was passing three arguments (email, password, username). This commit updates the AuthContextType definition to match the actual usage in the RegisterForm component, resolving the TypeScript error.
114 lines
4.0 KiB
TypeScript
114 lines
4.0 KiB
TypeScript
|
|
import { supabase } from '@/lib/supabase';
|
|
import { parseResponse, showAuthToast } from '@/utils/auth';
|
|
|
|
/**
|
|
* 직접 API 호출을 통한 회원가입
|
|
*/
|
|
export const signUpWithDirectApi = async (email: string, password: string, username: string) => {
|
|
try {
|
|
console.log('직접 API 호출로 회원가입 시도 중');
|
|
|
|
const supabaseUrl = supabase.auth.url;
|
|
const supabaseKey = localStorage.getItem('supabase_key') || supabase.supabaseKey;
|
|
|
|
// URL 경로 중복 방지를 위해 경로 확인 및 정규화
|
|
const baseUrl = supabaseUrl?.endsWith('/auth/v1')
|
|
? supabaseUrl
|
|
: `${supabaseUrl}/auth/v1`;
|
|
|
|
// 회원가입 API 엔드포인트 및 헤더 설정
|
|
const signUpUrl = `${baseUrl}/signup`;
|
|
const headers = {
|
|
'Content-Type': 'application/json',
|
|
'apikey': supabaseKey,
|
|
'X-Client-Info': 'supabase-js/2.x'
|
|
};
|
|
|
|
console.log('회원가입 API 요청:', signUpUrl);
|
|
|
|
// 회원가입 요청 전송
|
|
const response = await fetch(signUpUrl, {
|
|
method: 'POST',
|
|
headers,
|
|
body: JSON.stringify({
|
|
email,
|
|
password,
|
|
data: { username } // 사용자 메타데이터에 username 추가
|
|
})
|
|
});
|
|
|
|
console.log('회원가입 응답 상태:', response.status);
|
|
|
|
// HTTP 상태 코드 확인
|
|
if (response.status === 404) {
|
|
showAuthToast('회원가입 실패', '서버 경로를 찾을 수 없습니다. Supabase URL을 확인하세요.', 'destructive');
|
|
return {
|
|
error: { message: '서버 경로를 찾을 수 없습니다. Supabase URL을 확인하세요.' },
|
|
user: null
|
|
};
|
|
}
|
|
|
|
// 응답 처리
|
|
const responseData = await parseResponse(response);
|
|
|
|
if (responseData && responseData.error) {
|
|
const errorMessage = responseData.error_description || responseData.error || '회원가입 실패';
|
|
|
|
if (responseData.error === 'user_already_registered') {
|
|
showAuthToast('회원가입 실패', '이미 등록된 이메일 주소입니다.', 'destructive');
|
|
return { error: { message: '이미 등록된 이메일 주소입니다.' }, user: null };
|
|
}
|
|
|
|
showAuthToast('회원가입 실패', errorMessage, 'destructive');
|
|
return { error: { message: errorMessage }, user: null };
|
|
}
|
|
|
|
if (response.ok && responseData && responseData.id) {
|
|
const user = {
|
|
id: responseData.id,
|
|
email: responseData.email,
|
|
user_metadata: responseData.user_metadata || { username },
|
|
app_metadata: responseData.app_metadata || {},
|
|
created_at: responseData.created_at,
|
|
};
|
|
|
|
const confirmEmail = !responseData.confirmed_at;
|
|
|
|
if (confirmEmail) {
|
|
showAuthToast('회원가입 성공', '이메일 인증을 완료해주세요.', 'default');
|
|
return {
|
|
error: null,
|
|
user,
|
|
message: '이메일 인증 필요',
|
|
emailConfirmationRequired: true
|
|
};
|
|
} else {
|
|
showAuthToast('회원가입 성공', '환영합니다!', 'default');
|
|
|
|
// 성공 시 바로 로그인 세션 설정 시도
|
|
try {
|
|
await supabase.auth.signInWithPassword({ email, password });
|
|
} catch (loginError) {
|
|
console.warn('자동 로그인 실패:', loginError);
|
|
// 무시하고 계속 진행 (회원가입은 성공)
|
|
}
|
|
|
|
return { error: null, user };
|
|
}
|
|
} else {
|
|
// 예상치 못한 응답 형식
|
|
const errorMessage = '회원가입 처리 중 오류가 발생했습니다. 나중에 다시 시도하세요.';
|
|
showAuthToast('회원가입 실패', errorMessage, 'destructive');
|
|
return { error: { message: errorMessage }, user: null };
|
|
}
|
|
} catch (error: any) {
|
|
console.error('회원가입 API 호출 중 예외 발생:', error);
|
|
|
|
const errorMessage = error.message || '알 수 없는 오류가 발생했습니다.';
|
|
showAuthToast('회원가입 오류', errorMessage, 'destructive');
|
|
|
|
return { error: { message: errorMessage }, user: null };
|
|
}
|
|
};
|