Update error messages

Update error messages to provide more clarity.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-15 13:27:38 +00:00
parent dc0e94d352
commit 76f9b3b4df
4 changed files with 393 additions and 97 deletions

View File

@@ -27,12 +27,10 @@ export const signIn = async (email: string, password: string) => {
showAuthToast('로그인 성공', '환영합니다!');
return { error: null, user: data.user };
} else if (error) {
// 기본 방식 실패 시 커스텀 로그인 시도
console.warn('기본 로그인 실패, 커스텀 방식 시도:', error.message);
// JSON 파싱 오류라면 직접 API 호출 시도
// JSON 파싱 오류인 경우 직접 API 호출 시도
if (error.message.includes('json') || error.message.includes('Unexpected end')) {
// 다음 단계에서 직접 API 호출
console.warn('기본 로그인 실패, 직접 API 호출 시도:', error.message);
return await signInWithDirectApi(email, password);
} else {
// 다른 종류의 오류는 그대로 반환
let errorMessage = error.message;
@@ -49,38 +47,98 @@ export const signIn = async (email: string, password: string) => {
}
} catch (basicAuthError) {
console.warn('기본 인증 방식 예외 발생:', basicAuthError);
// 기본 로그인 실패 시 아래 커스텀 방식 계속 진행
// 기본 로그인 실패 시 아래 직접 API 호출 방식 계속 진행
return await signInWithDirectApi(email, password);
}
// 기본 방식 실패 시 직접 API 호출
// 응답 예외 처리를 위한 래핑
try {
const response = await fetch(`${supabase.auth.url}/token?grant_type=password`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'apikey': supabase.supabaseKey,
'Authorization': `Bearer ${supabase.supabaseKey}`,
'X-Client-Info': 'supabase-js/2.x'
},
body: JSON.stringify({ email, password })
});
// 응답 상태 확인 및 로깅
console.log('로그인 응답 상태:', response.status);
// 응답 처리
const responseData = await parseResponse(response);
// 오류 응답 확인
if (responseData?.error) {
const errorMessage = responseData.error_description || responseData.error;
showAuthToast('로그인 실패', errorMessage, 'destructive');
return { error: { message: errorMessage }, user: null };
// 기본 방식이 성공적으로 완료되지 않았을 경우 직접 API 호출
return await signInWithDirectApi(email, password);
} catch (error: any) {
console.error('로그인 중 예외 발생:', error);
// 네트워크 오류 확인
const errorMessage = handleNetworkError(error);
showAuthToast('로그인 오류', errorMessage, 'destructive');
return { error: { message: errorMessage }, user: null };
}
};
// 직접 API 호출을 통한 로그인 시도 (대체 방법)
const signInWithDirectApi = async (email: string, password: string) => {
console.log('직접 API 호출로 로그인 시도');
try {
// 로그인 API 엔드포인트 URL과 헤더 준비
const tokenUrl = `${supabase.auth.url}/token?grant_type=password`;
const headers = {
'Content-Type': 'application/json',
'apikey': supabase.supabaseKey,
'Authorization': `Bearer ${supabase.supabaseKey}`,
'X-Client-Info': 'supabase-js/2.x'
};
console.log('로그인 API 요청 URL:', tokenUrl);
// 로그인 요청 보내기
const response = await fetch(tokenUrl, {
method: 'POST',
headers,
body: JSON.stringify({ email, password })
});
// 응답 상태 확인 및 로깅
console.log('로그인 응답 상태:', response.status);
// HTTP 상태 코드 확인
if (response.status === 401) {
showAuthToast('로그인 실패', '이메일 또는 비밀번호가 올바르지 않습니다.', 'destructive');
return {
error: { message: '인증 실패: 이메일 또는 비밀번호가 올바르지 않습니다.' },
user: null
};
}
if (response.status === 404) {
showAuthToast('로그인 실패', '서버 경로를 찾을 수 없습니다. Supabase URL을 확인하세요.', 'destructive');
return {
error: { message: '서버 경로를 찾을 수 없습니다. Supabase URL을 확인하세요.' },
user: null
};
}
// 응답 처리
const responseData = await parseResponse(response);
// 빈 응답이나 파싱 실패 시
if (responseData.status && !responseData.success && !responseData.error) {
if (response.status >= 200 && response.status < 300) {
// 성공 상태 코드이지만 응답 내용 없음
showAuthToast('로그인 상태 확인 필요', '서버가 성공 응답을 보냈지만 내용이 없습니다. 세션 상태를 확인하세요.');
// 사용자 세션 확인 시도
try {
const { data: userData } = await supabase.auth.getUser();
if (userData.user) {
showAuthToast('로그인 성공', '환영합니다!');
return { error: null, user: userData.user };
}
} catch (sessionCheckError) {
console.error('세션 확인 오류:', sessionCheckError);
}
}
// 응답 처리
if (response.ok && responseData?.access_token) {
}
// 오류 응답 확인
if (responseData?.error) {
const errorMessage = responseData.error_description || responseData.error;
showAuthToast('로그인 실패', errorMessage, 'destructive');
return { error: { message: errorMessage }, user: null };
}
// 로그인 성공 응답 처리
if (response.ok && responseData?.access_token) {
try {
// 로그인 성공 시 Supabase 세션 설정
await supabase.auth.setSession({
access_token: responseData.access_token,
@@ -95,31 +153,28 @@ export const signIn = async (email: string, password: string) => {
showAuthToast('로그인 성공', '환영합니다!');
return { error: null, user: userData.user };
} else {
// 오류 응답 처리
console.error('로그인 오류 응답:', responseData);
const errorMessage = responseData?.error_description ||
responseData?.error ||
'로그인에 실패했습니다. 이메일과 비밀번호를 확인하세요.';
showAuthToast('로그인 실패', errorMessage, 'destructive');
return { error: { message: errorMessage }, user: null };
} catch (sessionError) {
console.error('세션 설정 오류:', sessionError);
showAuthToast('로그인 후처리 오류', '로그인에 성공했지만 세션 설정에 실패했습니다.', 'destructive');
return { error: { message: '세션 설정 오류' }, user: null };
}
} catch (fetchError) {
console.error('로그인 요청 중 fetch 오류:', fetchError);
} else {
// 오류 응답이나 예상치 못한 응답 형식 처리
console.error('로그인 오류 응답:', responseData);
const errorMessage = handleNetworkError(fetchError);
showAuthToast('로그인 요청 실패', errorMessage, 'destructive');
const errorMessage = responseData?.error_description ||
responseData?.error ||
responseData?.message ||
'로그인에 실패했습니다. 이메일과 비밀번호를 확인하세요.';
showAuthToast('로그인 실패', errorMessage, 'destructive');
return { error: { message: errorMessage }, user: null };
}
} catch (error: any) {
console.error('로그인 중 예외 발생:', error);
} catch (fetchError) {
console.error('로그인 요청 중 fetch 오류:', fetchError);
// 네트워크 오류 확인
const errorMessage = handleNetworkError(error);
showAuthToast('로그인 오류', errorMessage, 'destructive');
const errorMessage = handleNetworkError(fetchError);
showAuthToast('로그인 요청 실패', errorMessage, 'destructive');
return { error: { message: errorMessage }, user: null };
}