mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-27 08:30:47 +08:00
feat: add google auth integration
This commit is contained in:
32
open-isle-cli/src/utils/google.js
Normal file
32
open-isle-cli/src/utils/google.js
Normal 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()
|
||||
}
|
||||
Reference in New Issue
Block a user