import { Capacitor } from '@capacitor/core' import { SocialLogin } from '@capgo/capacitor-social-login' /** * 네이티브(Capacitor) 구글 로그인. * 웹뷰에서는 GIS(팝업/FedCM)가 동작하지 않으므로, 앱에서는 이 네이티브 흐름을 사용한다. * 받은 ID 토큰은 웹 GIS 와 동일하게 백엔드 /api/auth/google 로 검증한다. * - Android: serverClientId(webClientId) 로 토큰을 받으므로 aud = 웹 클라 ID * - iOS: iOS 클라 ID 로 로그인 → aud = iOS 클라 ID (백엔드가 web/iOS 둘 다 허용) */ // iOS 구글 클라이언트 ID (공개값). iOS 네이티브 로그인용. const IOS_CLIENT_ID = '913571348421-5a9k5kosv2mfosskcc3tllv5h1h9nfrn.apps.googleusercontent.com' let initializedFor = '' /** 네이티브 플랫폼(iOS/Android) 여부 — 웹은 기존 GIS 사용. */ export function isNativeGoogleAvailable(): boolean { return Capacitor.isNativePlatform() } /** * 네이티브 구글 로그인 → ID 토큰 반환. * @param webClientId 백엔드가 aud 검증에 쓰는 웹 클라이언트 ID (serverClientId) */ export async function signInWithGoogle(webClientId: string): Promise { if (initializedFor !== webClientId) { const google: { webClientId: string iOSClientId?: string iOSServerClientId?: string } = { webClientId } if (Capacitor.getPlatform() === 'ios') { google.iOSClientId = IOS_CLIENT_ID google.iOSServerClientId = webClientId } await SocialLogin.initialize({ google }) initializedFor = webClientId } const res = await SocialLogin.login({ provider: 'google', options: { scopes: ['email', 'profile'] }, }) const result = res.result as { idToken?: string | null } if (!result?.idToken) { throw new Error('구글 ID 토큰을 받지 못했습니다.') } return result.idToken }