Refactor the supabase.ts file into smaller modules, separating test functionalities and default client configurations while preserving all code functionalities.
120 lines
4.0 KiB
TypeScript
120 lines
4.0 KiB
TypeScript
|
|
import { supabase } from './client';
|
|
|
|
// 테스트용 직접 로그인 함수 (디버깅 전용)
|
|
export const testSupabaseLogin = async (email: string, password: string) => {
|
|
try {
|
|
console.log('테스트 로그인 시도:', email);
|
|
|
|
const { data, error } = await supabase.auth.signInWithPassword({
|
|
email,
|
|
password
|
|
});
|
|
|
|
if (error) {
|
|
console.error('테스트 로그인 오류:', error);
|
|
return { success: false, error };
|
|
}
|
|
|
|
console.log('테스트 로그인 성공:', data);
|
|
return { success: true, data };
|
|
} catch (err) {
|
|
console.error('테스트 로그인 중 예외 발생:', err);
|
|
return { success: false, error: err };
|
|
}
|
|
};
|
|
|
|
// API 테스트 도우미 함수
|
|
export const testSupabaseConnection = async () => {
|
|
const results = {
|
|
url: getSupabaseUrl(),
|
|
client: !!supabase,
|
|
restApi: false,
|
|
auth: false,
|
|
database: false,
|
|
errors: [] as string[]
|
|
};
|
|
|
|
try {
|
|
// 1. REST API 접근 테스트
|
|
try {
|
|
console.log('REST API 테스트 시작...');
|
|
const response = await fetch(`${results.url}/rest/v1/`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'apikey': getSupabaseKey(),
|
|
},
|
|
});
|
|
|
|
results.restApi = response.ok;
|
|
if (!response.ok) {
|
|
const errorBody = await response.text();
|
|
results.errors.push(`REST API 오류(${response.status} ${response.statusText}): ${errorBody || '응답 없음'}`);
|
|
console.error('REST API 테스트 실패:', response.status, errorBody);
|
|
} else {
|
|
console.log('REST API 테스트 성공');
|
|
}
|
|
} catch (err: any) {
|
|
results.errors.push(`REST API 예외: ${err.message || '알 수 없는 오류'}`);
|
|
console.error('REST API 테스트 중 예외:', err);
|
|
}
|
|
|
|
// 2. 인증 서비스 테스트
|
|
try {
|
|
console.log('인증 서비스 테스트 시작...');
|
|
const { data, error } = await supabase.auth.getSession();
|
|
results.auth = !error;
|
|
if (error) {
|
|
results.errors.push(`인증 오류: ${error.message}`);
|
|
console.error('인증 테스트 실패:', error);
|
|
} else {
|
|
console.log('인증 테스트 성공');
|
|
}
|
|
} catch (err: any) {
|
|
results.errors.push(`인증 예외: ${err.message || '알 수 없는 오류'}`);
|
|
console.error('인증 테스트 중 예외:', err);
|
|
}
|
|
|
|
// 3. 데이터베이스 연결 테스트
|
|
try {
|
|
console.log('데이터베이스 연결 테스트 시작...');
|
|
const { data, error } = await supabase
|
|
.from('transactions')
|
|
.select('*')
|
|
.limit(1);
|
|
|
|
results.database = !error;
|
|
if (error) {
|
|
results.errors.push(`데이터베이스 오류: ${error.message}`);
|
|
console.error('데이터베이스 테스트 실패:', error);
|
|
} else {
|
|
console.log('데이터베이스 테스트 성공', data);
|
|
}
|
|
} catch (err: any) {
|
|
results.errors.push(`데이터베이스 예외: ${err.message || '알 수 없는 오류'}`);
|
|
console.error('데이터베이스 테스트 중 예외:', err);
|
|
}
|
|
|
|
// 오류가 없는 경우 메시지 추가
|
|
if (results.errors.length === 0) {
|
|
results.errors.push('모든 테스트 통과! 연결 상태가 정상입니다.');
|
|
}
|
|
} catch (err: any) {
|
|
results.errors.push(`테스트 실행 예외: ${err.message || '알 수 없는 오류'}`);
|
|
console.error('전체 테스트 실행 중 예외:', err);
|
|
}
|
|
|
|
console.log('Supabase 연결 테스트 결과:', results);
|
|
return results;
|
|
};
|
|
|
|
// 필요한 함수 불러오기
|
|
function getSupabaseUrl() {
|
|
return localStorage.getItem('supabase_url') || process.env.SUPABASE_URL || 'http://a11.ism.kr:8000';
|
|
}
|
|
|
|
function getSupabaseKey() {
|
|
return localStorage.getItem('supabase_key') || process.env.SUPABASE_ANON_KEY || 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiYW5vbiIsImlzcyI6InN1cGFiYXNlIiwiaWF0IjoxNzQyMDM5MzU4LCJleHAiOjQ4OTU2MzkzNTh9.XK0vetdwv_H2MHj4ewTfZGtSbrbSNDaV5xJhNo_Hdp8';
|
|
}
|