feat: add google auth integration

This commit is contained in:
Tim
2025-07-05 14:51:29 +08:00
parent e5ee4fcabb
commit ed12fb0fc9
6 changed files with 49 additions and 2 deletions

View File

@@ -0,0 +1,32 @@
import { API_BASE_URL, GOOGLE_CLIENT_ID, toast } from '../main'
import { setToken } from './auth'
export function googleSignIn(redirect) {
if (!window.google || !GOOGLE_CLIENT_ID) {
toast.error('Google 登录不可用')
return
}
window.google.accounts.id.initialize({
client_id: GOOGLE_CLIENT_ID,
callback: async ({ credential }) => {
try {
const res = await fetch(`${API_BASE_URL}/api/auth/google`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ idToken: credential })
})
const data = await res.json()
if (res.ok && data.token) {
setToken(data.token)
toast.success('登录成功')
if (redirect) redirect()
} else {
toast.error(data.error || '登录失败')
}
} catch (e) {
toast.error('登录失败')
}
}
})
window.google.accounts.id.prompt()
}