Files
dognation_PT/src/lib/googleAuth.ts
T

36 lines
1.2 KiB
TypeScript
Raw Normal View History

import { Capacitor } from '@capacitor/core'
import { SocialLogin } from '@capgo/capacitor-social-login'
/**
* 네이티브(Capacitor) 구글 로그인.
* 웹뷰에서는 GIS(팝업/FedCM)가 동작하지 않으므로, 앱에서는 이 네이티브 흐름을 사용한다.
* 받은 ID 토큰은 웹 GIS 와 동일하게 백엔드 /api/auth/google 로 검증한다.
*/
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<string> {
if (initializedFor !== webClientId) {
await SocialLogin.initialize({ google: { webClientId } })
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
}