Add Discord OAuth login

This commit is contained in:
Tim
2025-07-16 18:03:08 +08:00
parent fa95328f6d
commit 18d25e303a
11 changed files with 283 additions and 0 deletions

View File

@@ -0,0 +1 @@
<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Discord</title><path d="M20.317 4.3698a19.7913 19.7913 0 00-4.8851-1.5152.0741.0741 0 00-.0785.0371c-.211.3753-.4447.8648-.6083 1.2495-1.8447-.2762-3.68-.2762-5.4868 0-.1636-.3933-.4058-.8742-.6177-1.2495a.077.077 0 00-.0785-.037 19.7363 19.7363 0 00-4.8852 1.515.0699.0699 0 00-.0321.0277C.5334 9.0458-.319 13.5799.0992 18.0578a.0824.0824 0 00.0312.0561c2.0528 1.5076 4.0413 2.4228 5.9929 3.0294a.0777.0777 0 00.0842-.0276c.4616-.6304.8731-1.2952 1.226-1.9942a.076.076 0 00-.0416-.1057c-.6528-.2476-1.2743-.5495-1.8722-.8923a.077.077 0 01-.0076-.1277c.1258-.0943.2517-.1923.3718-.2914a.0743.0743 0 01.0776-.0105c3.9278 1.7933 8.18 1.7933 12.0614 0a.0739.0739 0 01.0785.0095c.1202.099.246.1981.3728.2924a.077.077 0 01-.0066.1276 12.2986 12.2986 0 01-1.873.8914.0766.0766 0 00-.0407.1067c.3604.698.7719 1.3628 1.225 1.9932a.076.076 0 00.0842.0286c1.961-.6067 3.9495-1.5219 6.0023-3.0294a.077.077 0 00.0313-.0552c.5004-5.177-.8382-9.6739-3.5485-13.6604a.061.061 0 00-.0312-.0286zM8.02 15.3312c-1.1825 0-2.1569-1.0857-2.1569-2.419 0-1.3332.9555-2.4189 2.157-2.4189 1.2108 0 2.1757 1.0952 2.1568 2.419 0 1.3332-.9555 2.4189-2.1569 2.4189zm7.9748 0c-1.1825 0-2.1569-1.0857-2.1569-2.419 0-1.3332.9554-2.4189 2.1569-2.4189 1.2108 0 2.1757 1.0952 2.1568 2.419 0 1.3332-.946 2.4189-2.1568 2.4189Z"/></svg>

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@@ -19,6 +19,7 @@ export const API_PORT = 8081
export const API_BASE_URL = "";
export const GOOGLE_CLIENT_ID = '777830451304-nt8afkkap18gui4f9entcha99unal744.apps.googleusercontent.com'
export const GITHUB_CLIENT_ID = 'Ov23liVkO1NPAX5JyWxJ'
export const DISCORD_CLIENT_ID = ''
export const toast = useToast()
initTheme()

View File

@@ -12,6 +12,7 @@ import SettingsPageView from '../views/SettingsPageView.vue'
import ProfileView from '../views/ProfileView.vue'
import NotFoundPageView from '../views/NotFoundPageView.vue'
import GithubCallbackPageView from '../views/GithubCallbackPageView.vue'
import DiscordCallbackPageView from '../views/DiscordCallbackPageView.vue'
const routes = [
{
@@ -74,6 +75,11 @@ const routes = [
name: 'github-callback',
component: GithubCallbackPageView
},
{
path: '/discord-callback',
name: 'discord-callback',
component: DiscordCallbackPageView
},
{
path: '/404',
name: 'not-found',

View File

@@ -0,0 +1,59 @@
import { API_BASE_URL, DISCORD_CLIENT_ID, toast } from '../main'
import { setToken, loadCurrentUser } from './auth'
export function discordAuthorize(state = '') {
if (!DISCORD_CLIENT_ID) {
toast.error('Discord 登录不可用')
return
}
const redirectUri = `${window.location.origin}/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}`
window.location.href = url
}
export async function discordExchange(code, state, reason) {
try {
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 })
})
const data = await res.json()
if (res.ok && data.token) {
setToken(data.token)
await loadCurrentUser()
toast.success('登录成功')
return {
success: true,
needReason: false
}
} else if (data.reason_code === 'NOT_APPROVED') {
toast.info('当前为注册审核模式,请填写注册理由')
return {
success: false,
needReason: true,
token: data.token
}
} else if (data.reason_code === 'IS_APPROVING') {
toast.info('您的注册理由正在审批中')
return {
success: true,
needReason: false
}
} else {
toast.error(data.error || '登录失败')
return {
success: false,
needReason: false,
error: data.error || '登录失败'
}
}
} catch (e) {
toast.error('登录失败')
return {
success: false,
needReason: false,
error: '登录失败'
}
}
}

View File

@@ -0,0 +1,47 @@
<template>
<div class="discord-callback-page">
<l-hatch size="28" stroke="4" speed="3.5" color="var(--primary-color)"></l-hatch>
<div class="discord-callback-page-text">Magic is happening...</div>
</div>
</template>
<script>
import { discordExchange } from '../utils/discord'
import { hatch } from 'ldrs'
hatch.register()
export default {
name: 'DiscordCallbackPageView',
async mounted() {
const url = new URL(window.location.href)
const code = url.searchParams.get('code')
const state = url.searchParams.get('state')
const result = await discordExchange(code, state, '')
if (result.needReason) {
this.$router.push('/signup-reason?token=' + result.token)
} else {
this.$router.push('/')
}
}
}
</script>
<style scoped>
.discord-callback-page {
background-color: var(--background-color);
height: calc(100vh - var(--header-height));
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.discord-callback-page-text {
margin-top: 25px;
font-size: 16px;
color: var(--primary-color);
font-weight: bold;
}
</style>

View File

@@ -38,6 +38,10 @@
<img class="login-page-button-icon" src="../assets/icons/github.svg" alt="GitHub Logo" />
<div class="login-page-button-text">GitHub 登录</div>
</div>
<div class="login-page-button" @click="loginWithDiscord">
<img class="login-page-button-icon" src="../assets/icons/discord.svg" alt="Discord Logo" />
<div class="login-page-button-text">Discord 登录</div>
</div>
</div>
</div>
</template>
@@ -47,6 +51,7 @@ import { API_BASE_URL, toast } from '../main'
import { setToken, loadCurrentUser } from '../utils/auth'
import { googleSignIn } from '../utils/google'
import { githubAuthorize } from '../utils/github'
import { discordAuthorize } from '../utils/discord'
import BaseInput from '../components/BaseInput.vue'
export default {
name: 'LoginPageView',
@@ -101,6 +106,9 @@ export default {
},
loginWithGithub() {
githubAuthorize()
},
loginWithDiscord() {
discordAuthorize()
}
}
}

View File

@@ -80,6 +80,10 @@
<img class="signup-page-button-icon" src="../assets/icons/github.svg" alt="GitHub Logo" />
<div class="signup-page-button-text">GitHub 注册</div>
</div>
<div class="signup-page-button" @click="signupWithDiscord">
<img class="signup-page-button-icon" src="../assets/icons/discord.svg" alt="Discord Logo" />
<div class="signup-page-button-text">Discord 注册</div>
</div>
</div>
</div>
</template>
@@ -88,6 +92,7 @@
import { API_BASE_URL, toast } from '../main'
import { googleSignIn } from '../utils/google'
import { githubAuthorize } from '../utils/github'
import { discordAuthorize } from '../utils/discord'
import BaseInput from '../components/BaseInput.vue'
export default {
name: 'SignupPageView',
@@ -208,6 +213,9 @@ export default {
},
signupWithGithub() {
githubAuthorize()
},
signupWithDiscord() {
discordAuthorize()
}
}
}