mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-24 23:20:49 +08:00
feat: fix with google login
This commit is contained in:
@@ -3,6 +3,22 @@ import { setToken, loadCurrentUser } from './auth'
|
||||
import { registerPush } from './push'
|
||||
import { WEBSITE_BASE_URL } from '../constants'
|
||||
|
||||
export async function googleGetIdToken() {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!window.google || !GOOGLE_CLIENT_ID) {
|
||||
toast.error('Google 登录不可用, 请检查网络设置与VPN')
|
||||
reject()
|
||||
return
|
||||
}
|
||||
window.google.accounts.id.initialize({
|
||||
client_id: GOOGLE_CLIENT_ID,
|
||||
callback: ({ credential }) => resolve(credential),
|
||||
use_fedcm: true
|
||||
})
|
||||
window.google.accounts.id.prompt()
|
||||
})
|
||||
}
|
||||
|
||||
export function googleAuthorize() {
|
||||
if (!GOOGLE_CLIENT_ID) {
|
||||
toast.error('Google 登录不可用, 请检查网络设置与VPN')
|
||||
@@ -11,7 +27,7 @@ export function googleAuthorize() {
|
||||
const redirectUri = `${WEBSITE_BASE_URL}/google-callback`
|
||||
const nonce = Math.random().toString(36).substring(2)
|
||||
const url = `https://accounts.google.com/o/oauth2/v2/auth?client_id=${GOOGLE_CLIENT_ID}&redirect_uri=${encodeURIComponent(redirectUri)}&response_type=id_token&scope=openid%20email%20profile&nonce=${nonce}`
|
||||
window.open(url, '_blank', 'width=500,height=600')
|
||||
window.location.href = url
|
||||
}
|
||||
|
||||
export async function googleAuthWithToken(idToken, redirect_success, redirect_not_approved) {
|
||||
@@ -38,4 +54,26 @@ export async function googleAuthWithToken(idToken, redirect_success, redirect_no
|
||||
} catch (e) {
|
||||
toast.error('登录失败')
|
||||
}
|
||||
}
|
||||
|
||||
export async function googleSignIn(redirect_success, redirect_not_approved) {
|
||||
try {
|
||||
const token = await googleGetIdToken()
|
||||
await googleAuthWithToken(token, redirect_success, redirect_not_approved)
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
import router from '../router'
|
||||
|
||||
export function loginWithGoogle() {
|
||||
googleSignIn(
|
||||
() => {
|
||||
router.push('/')
|
||||
},
|
||||
token => {
|
||||
router.push('/signup-reason?token=' + token)
|
||||
}
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user