import { AuthResponse, OAuthProvider } from "@/types";
import { AuthRequest, AuthSessionResult } from "expo-auth-session";
import { AuthService } from "./authService.native";
/**
* OAuth 인증 처리 서비스
*/
export class OAuthService {
/**
* OAuth 인증 응답을 처리하고 토큰을 반환하는 함수 (mobile)
*/
static async processAuthResponse(
response: AuthSessionResult | null,
request: AuthRequest | null,
provider: OAuthProvider
): Promise<AuthResponse> {
if (!response || response.type !== "success") {
return {
success: false,
error: `${provider} 인증이 실패했습니다`
};
}
const { id_token } = response.params;
if (!id_token || !request?.redirectUri) {
return {
success: false,
error: `${provider} 인증 요청이 유효하지 않습니다`
};
}
if (provider === "google") {
return await AuthService.loginWithGoogle({
token: id_token,
redirectUri: request.redirectUri,
});
} else if (provider === "apple") {
// Apple 로그인 구현 예정
return {
success: false,
error: "Apple 로그인은 아직 구현되지 않았습니다"
};
} else {
const _exhaustiveCheck: never = provider;
return {
success: false,
error: `지원하지 않는 인증 제공자입니다: ${_exhaustiveCheck}`
};
}
}
/**
* OAuth 프롬프트를 시작하는 함수
*/
static async startAuthPrompt(
promptAsync: () => Promise<AuthSessionResult>,
provider: OAuthProvider
): Promise<AuthResponse> {
try {
await promptAsync();
return { success: true };
} catch (error) {
console.error(`${provider} 인증 요청 실패:`, error);
return {
success: false,
error: `${provider} 인증 요청에 실패했습니다`
};
}
}
}