mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-22 17:31:15 +08:00
Merge pull request #211 from nagisa77/codex/optimize-google-registration-process
Improve login error handling and Google whitelist signup
This commit is contained in:
@@ -1,33 +1,47 @@
|
||||
import { API_BASE_URL, GOOGLE_CLIENT_ID, toast } from '../main'
|
||||
import { setToken, loadCurrentUser } from './auth'
|
||||
|
||||
export function googleSignIn(redirect, reason) {
|
||||
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, reason })
|
||||
})
|
||||
const data = await res.json()
|
||||
if (res.ok && data.token) {
|
||||
setToken(data.token)
|
||||
await loadCurrentUser()
|
||||
toast.success('登录成功')
|
||||
if (redirect) redirect()
|
||||
} else {
|
||||
toast.error(data.error || '登录失败')
|
||||
}
|
||||
} catch (e) {
|
||||
toast.error('登录失败')
|
||||
}
|
||||
export async function googleGetIdToken() {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!window.google || !GOOGLE_CLIENT_ID) {
|
||||
toast.error('Google 登录不可用')
|
||||
reject()
|
||||
return
|
||||
}
|
||||
window.google.accounts.id.initialize({
|
||||
client_id: GOOGLE_CLIENT_ID,
|
||||
callback: ({ credential }) => resolve(credential)
|
||||
})
|
||||
window.google.accounts.id.prompt()
|
||||
})
|
||||
window.google.accounts.id.prompt()
|
||||
}
|
||||
|
||||
export async function googleAuthWithToken(idToken, reason, redirect) {
|
||||
try {
|
||||
const res = await fetch(`${API_BASE_URL}/api/auth/google`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ idToken, reason })
|
||||
})
|
||||
const data = await res.json()
|
||||
if (res.ok && data.token) {
|
||||
setToken(data.token)
|
||||
await loadCurrentUser()
|
||||
toast.success('登录成功')
|
||||
if (redirect) redirect()
|
||||
} else {
|
||||
toast.error(data.error || '登录失败')
|
||||
}
|
||||
} catch (e) {
|
||||
toast.error('登录失败')
|
||||
}
|
||||
}
|
||||
|
||||
export async function googleSignIn(redirect, reason) {
|
||||
try {
|
||||
const token = await googleGetIdToken()
|
||||
await googleAuthWithToken(token, reason, redirect)
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@
|
||||
|
||||
<script>
|
||||
import { API_BASE_URL, toast } from '../main'
|
||||
import { googleSignIn } from '../utils/google'
|
||||
import { googleSignIn, googleGetIdToken } from '../utils/google'
|
||||
import BaseInput from '../components/BaseInput.vue'
|
||||
export default {
|
||||
name: 'SignupPageView',
|
||||
@@ -197,7 +197,10 @@ export default {
|
||||
},
|
||||
signupWithGoogle() {
|
||||
if (this.registerMode === 'WHITELIST') {
|
||||
this.$router.push('/signup-reason?google=1')
|
||||
googleGetIdToken().then(token => {
|
||||
sessionStorage.setItem('google_id_token', token)
|
||||
this.$router.push('/signup-reason?google=1')
|
||||
}).catch(() => {})
|
||||
} else {
|
||||
googleSignIn(() => {
|
||||
this.$router.push('/')
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
<script>
|
||||
import BaseInput from '../components/BaseInput.vue'
|
||||
import { API_BASE_URL, toast } from '../main'
|
||||
import { googleSignIn } from '../utils/google'
|
||||
import { googleAuthWithToken } from '../utils/google'
|
||||
|
||||
export default {
|
||||
name: 'SignupReasonPageView',
|
||||
@@ -25,16 +25,20 @@ export default {
|
||||
return {
|
||||
reason: '',
|
||||
error: '',
|
||||
isGoogle: false,
|
||||
isWaitingForRegister: false
|
||||
isGoogle: false,
|
||||
isWaitingForRegister: false,
|
||||
googleToken: ''
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
this.isGoogle = this.$route.query.google === '1'
|
||||
if (!this.isGoogle) {
|
||||
if (!sessionStorage.getItem('signup_username')) {
|
||||
if (this.isGoogle) {
|
||||
this.googleToken = sessionStorage.getItem('google_id_token') || ''
|
||||
if (!this.googleToken) {
|
||||
this.$router.push('/signup')
|
||||
}
|
||||
} else if (!sessionStorage.getItem('signup_username')) {
|
||||
this.$router.push('/signup')
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
@@ -44,7 +48,13 @@ export default {
|
||||
return
|
||||
}
|
||||
if (this.isGoogle) {
|
||||
googleSignIn(() => { this.$router.push('/') }, this.reason)
|
||||
const token = this.googleToken || sessionStorage.getItem('google_id_token')
|
||||
if (!token) {
|
||||
toast.error('Google 登录失败')
|
||||
return
|
||||
}
|
||||
await googleAuthWithToken(token, this.reason, () => { this.$router.push('/') })
|
||||
sessionStorage.removeItem('google_id_token')
|
||||
return
|
||||
}
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user