mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-05-31 06:57:35 +08:00
feat: 各种登录方式传入invite_token
This commit is contained in:
@@ -2,7 +2,7 @@ import { toast } from '../main'
|
||||
import { setToken, loadCurrentUser } from './auth'
|
||||
import { registerPush } from './push'
|
||||
|
||||
export function discordAuthorize(state = '') {
|
||||
export function discordAuthorize(inviteToken = '') {
|
||||
const config = useRuntimeConfig()
|
||||
const WEBSITE_BASE_URL = config.public.websiteBaseUrl
|
||||
const DISCORD_CLIENT_ID = config.public.discordClientId
|
||||
@@ -10,62 +10,60 @@ export function discordAuthorize(state = '') {
|
||||
toast.error('Discord 登录不可用')
|
||||
return
|
||||
}
|
||||
|
||||
const redirectUri = `${WEBSITE_BASE_URL}/discord-callback`
|
||||
const url = `https://discord.com/api/oauth2/authorize?client_id=${DISCORD_CLIENT_ID}&redirect_uri=${encodeURIComponent(redirectUri)}&response_type=code&scope=identify%20email&state=${state}`
|
||||
// 用 state 明文携带 invite_token(仅用于回传,不再透传给后端)
|
||||
const state = new URLSearchParams({ invite_token: inviteToken }).toString()
|
||||
|
||||
const url =
|
||||
`https://discord.com/api/oauth2/authorize` +
|
||||
`?client_id=${encodeURIComponent(DISCORD_CLIENT_ID)}` +
|
||||
`&redirect_uri=${encodeURIComponent(redirectUri)}` +
|
||||
`&response_type=code` +
|
||||
`&scope=${encodeURIComponent('identify email')}` +
|
||||
`&state=${encodeURIComponent(state)}`
|
||||
|
||||
window.location.href = url
|
||||
}
|
||||
|
||||
export async function discordExchange(code, state, reason) {
|
||||
export async function discordExchange(code, inviteToken = '', reason = '') {
|
||||
try {
|
||||
const config = useRuntimeConfig()
|
||||
const API_BASE_URL = config.public.apiBaseUrl
|
||||
|
||||
const payload = {
|
||||
code,
|
||||
redirectUri: `${window.location.origin}/discord-callback`,
|
||||
reason,
|
||||
}
|
||||
if (inviteToken) payload.inviteToken = inviteToken // 明文传给后端
|
||||
|
||||
const res = await fetch(`${API_BASE_URL}/api/auth/discord`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
code,
|
||||
redirectUri: `${window.location.origin}/discord-callback`,
|
||||
reason,
|
||||
state,
|
||||
}),
|
||||
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
})
|
||||
const data = await res.json()
|
||||
|
||||
if (res.ok && data.token) {
|
||||
setToken(data.token)
|
||||
await loadCurrentUser()
|
||||
toast.success('登录成功')
|
||||
registerPush()
|
||||
return {
|
||||
success: true,
|
||||
needReason: false,
|
||||
}
|
||||
registerPush?.()
|
||||
return { success: true, needReason: false }
|
||||
} else if (data.reason_code === 'NOT_APPROVED') {
|
||||
toast.info('当前为注册审核模式,请填写注册理由')
|
||||
return {
|
||||
success: false,
|
||||
needReason: true,
|
||||
token: data.token,
|
||||
}
|
||||
return { success: false, needReason: true, token: data.token }
|
||||
} else if (data.reason_code === 'IS_APPROVING') {
|
||||
toast.info('您的注册理由正在审批中')
|
||||
return {
|
||||
success: true,
|
||||
needReason: false,
|
||||
}
|
||||
return { success: true, needReason: false }
|
||||
} else {
|
||||
toast.error(data.error || '登录失败')
|
||||
return {
|
||||
success: false,
|
||||
needReason: false,
|
||||
error: data.error || '登录失败',
|
||||
}
|
||||
return { success: false, needReason: false, error: data.error || '登录失败' }
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
toast.error('登录失败')
|
||||
return {
|
||||
success: false,
|
||||
needReason: false,
|
||||
error: '登录失败',
|
||||
}
|
||||
return { success: false, needReason: false, error: '登录失败' }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user