feat: 登录改为 Casdoor 统一身份认证(SSO),重建用户体系

- 移除内置密码注册/登录/改密/删号/头像,统一走 Casdoor OIDC 授权码
- 服务端 casdoor-nodejs-sdk:code 换 token + 证书公钥验签 + get-account 补齐资料
- 用户表重建为 provider+provider_id(Casdoor sub 为准),邮箱不再唯一
- 管理员由 Casdoor token 的 isAdmin/IsAdmin 字段或 admin 角色判定,删除角色切换接口
- 前端登录弹窗改为申统认证跳转,注册去 Casdoor;后台用户列表只读角色
- 会话沿用 an_session httpOnly Cookie,requireUser/requireAdmin 守卫保留
- 配置注入 deploy.sh/.env.example;新增 casdoor 单测,删除 password 单测
This commit is contained in:
2026-08-09 00:18:47 +08:00
parent bdafac2857
commit 337eb4f05a
51 changed files with 1395 additions and 1504 deletions
+30
View File
@@ -0,0 +1,30 @@
<template>
<!-- 有图显示图片;无图显示品牌渐变兜底 + 标题文字,避免裂图 -->
<img
v-if="src"
:src="src"
:alt="alt"
v-bind="$attrs"
loading="lazy"
decoding="async"
/>
<div
v-else
v-bind="$attrs"
class="flex items-center justify-center bg-gradient-to-br from-gray-200 via-gray-100 to-gray-200"
>
<span class="px-4 text-center text-sm md:text-base font-bold tracking-tight text-gray-400">
{{ title }}
</span>
</div>
</template>
<script setup lang="ts">
defineOptions({ inheritAttrs: false })
defineProps<{
src?: string | null
alt?: string
title?: string
}>()
</script>
+32 -87
View File
@@ -7,7 +7,6 @@
@click.self="closeModal"
>
<div
ref="modalRef"
class="relative w-full max-w-md bg-white rounded-2xl shadow-2xl overflow-hidden"
@click.stop
>
@@ -20,69 +19,30 @@
<Icon name="ph:x" class="w-5 h-5 text-gray-600" />
</button>
<!-- 登录表单 -->
<!-- 统一身份认证 -->
<div class="p-8">
<h2 class="text-2xl md:text-3xl font-bold text-black mb-2">{{ t('auth.login') }}</h2>
<p class="text-gray-600 mb-8">{{ t('auth.loginDescription') }}</p>
<p class="text-gray-600 mb-8">{{ t('auth.casdoorDescription') }}</p>
<form @submit.prevent="handleLogin" class="space-y-5">
<div>
<label for="account" class="block text-sm font-semibold text-gray-800 mb-2">
{{ t('auth.account') }}
</label>
<input
id="account"
v-model="account"
type="text"
required
autocomplete="username"
:disabled="isSubmitting"
class="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-black focus:ring-2 focus:ring-black/5 outline-none transition-all duration-300 disabled:opacity-50 disabled:cursor-not-allowed"
:placeholder="t('auth.accountPlaceholder')"
/>
</div>
<p class="text-sm text-gray-500 mb-4">{{ t('auth.casdoorHint') }}</p>
<div>
<label for="password" class="block text-sm font-semibold text-gray-800 mb-2">
{{ t('auth.password') }}
</label>
<input
id="password"
v-model="password"
type="password"
required
autocomplete="current-password"
:disabled="isSubmitting"
class="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-black focus:ring-2 focus:ring-black/5 outline-none transition-all duration-300 disabled:opacity-50 disabled:cursor-not-allowed"
:placeholder="t('auth.passwordPlaceholder')"
/>
</div>
<!-- 登录按钮:跳转 Casdoor,回跳当前页面 -->
<button
type="button"
:disabled="isRedirecting"
class="w-full py-3 rounded-xl font-semibold text-white transition-all duration-300 flex items-center justify-center gap-2 bg-black hover:bg-gray-800 shadow-lg hover:shadow-xl disabled:opacity-60 disabled:cursor-not-allowed"
@click="handleLogin"
>
<Icon name="ph:shield-check" class="w-5 h-5" />
<span>{{ isRedirecting ? t('auth.redirecting') : t('auth.loginWithCasdoor') }}</span>
</button>
<!-- 错误消息 -->
<div v-if="errorMessage" class="p-3 rounded-lg bg-red-50 border border-red-200 text-red-800 text-sm">
{{ errorMessage }}
</div>
<!-- 登录按钮 -->
<button
type="submit"
:disabled="isSubmitting || !account || !password"
class="w-full py-3 rounded-xl font-semibold text-white transition-all duration-300 flex items-center justify-center gap-2"
:class="isSubmitting || !account || !password
? 'bg-gray-400 cursor-not-allowed'
: 'bg-black hover:bg-gray-800 shadow-lg hover:shadow-xl'"
>
<Icon v-if="isSubmitting" name="ph:circle-notch" class="w-5 h-5 animate-spin" />
<span>{{ isSubmitting ? t('auth.loggingIn') : t('auth.loginButton') }}</span>
</button>
</form>
<!-- 注册账号链接 -->
<!-- 注册:前往 Casdoor 注册页 -->
<p class="mt-6 text-center text-sm text-gray-600">
{{ t('auth.noAccount') }}
<button @click="authModal.switchToRegister()" class="text-black font-semibold hover:underline">
<a :href="signupUrl" target="_blank" rel="noopener" class="text-black font-semibold hover:underline">
{{ t('auth.createAccount') }}
</button>
</a>
</p>
</div>
</div>
@@ -93,54 +53,39 @@
<script setup lang="ts">
import { ref, watch } from 'vue'
import { useRoute } from '#app'
import { useI18n } from 'vue-i18n'
import { useUserStore } from '~/stores/user'
import { useAuthModalStore } from '~/stores/authModal'
import { useApiError } from '~/composables/useApiError'
import { getCasdoorSignupUrl } from '~/utils/casdoorClient'
const { t } = useI18n()
const userStore = useUserStore()
const route = useRoute()
const authModal = useAuthModalStore()
const apiError = useApiError()
const account = ref('')
const password = ref('')
const isSubmitting = ref(false)
const errorMessage = ref('')
const isRedirecting = ref(false)
const signupUrl = getCasdoorSignupUrl()
const closeModal = () => {
if (!isSubmitting.value) {
if (!isRedirecting.value) {
authModal.closeAll()
}
}
// 打开时重置表单
// 打开时重置状态
watch(
() => authModal.isLoginOpen,
(open) => {
if (open) {
account.value = ''
password.value = ''
errorMessage.value = ''
}
if (open) isRedirecting.value = false
},
)
const handleLogin = async () => {
if (isSubmitting.value) return
isSubmitting.value = true
errorMessage.value = ''
const result = await userStore.login(account.value, password.value)
if (result.success) {
account.value = ''
password.value = ''
authModal.closeAll()
} else {
errorMessage.value = apiError({ data: { code: result.code } })
}
isSubmitting.value = false
const handleLogin = () => {
if (isRedirecting.value) return
isRedirecting.value = true
const next = route.fullPath
const target = next.startsWith('/') && !next.startsWith('//') ? next : '/'
const url = `/api/auth/casdoor-login?next=${encodeURIComponent(target)}`
window.location.assign(url)
}
// ESC 关闭
@@ -179,4 +124,4 @@ if (typeof window !== 'undefined') {
transform: scale(0.95) translateY(20px);
opacity: 0;
}
</style>
</style>
-263
View File
@@ -1,263 +0,0 @@
<template>
<Teleport to="body">
<Transition name="modal">
<div
v-if="authModal.isRegisterOpen"
class="fixed inset-0 z-[100] flex items-center justify-center bg-black/50 backdrop-blur-sm px-4"
@click.self="closeModal"
>
<div
ref="modalRef"
class="relative w-full max-w-md bg-white rounded-2xl shadow-2xl overflow-hidden"
@click.stop
>
<!-- 关闭按钮 -->
<button
@click="closeModal"
class="absolute top-4 right-4 w-8 h-8 flex items-center justify-center rounded-full hover:bg-gray-100 transition-colors z-10"
aria-label="Close"
>
<Icon name="ph:x" class="w-5 h-5 text-gray-600" />
</button>
<!-- 注册表单 -->
<div class="p-8">
<h2 class="text-2xl md:text-3xl font-bold text-black mb-2">{{ t('auth.register') }}</h2>
<p class="text-gray-600 mb-8">{{ t('auth.registerDescription') }}</p>
<form @submit.prevent="handleRegister" class="space-y-5">
<div>
<label for="reg-username" class="block text-sm font-semibold text-gray-800 mb-2">
{{ t('auth.username') }}
</label>
<input
id="reg-username"
v-model="username"
type="text"
required
autocomplete="username"
:disabled="isSubmitting"
class="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-black focus:ring-2 focus:ring-black/5 outline-none transition-all duration-300 disabled:opacity-50 disabled:cursor-not-allowed"
:placeholder="t('auth.usernamePlaceholder')"
/>
</div>
<div>
<label for="reg-email" class="block text-sm font-semibold text-gray-800 mb-2">
{{ t('auth.email') }}
</label>
<input
id="reg-email"
v-model="email"
type="email"
required
autocomplete="email"
:disabled="isSubmitting"
class="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-black focus:ring-2 focus:ring-black/5 outline-none transition-all duration-300 disabled:opacity-50 disabled:cursor-not-allowed"
:placeholder="t('auth.emailPlaceholder')"
/>
</div>
<div>
<label for="reg-phone" class="block text-sm font-semibold text-gray-800 mb-2">
{{ t('auth.phone') }}
</label>
<input
id="reg-phone"
v-model="phone"
type="tel"
autocomplete="tel"
:disabled="isSubmitting"
class="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-black focus:ring-2 focus:ring-black/5 outline-none transition-all duration-300 disabled:opacity-50 disabled:cursor-not-allowed"
:placeholder="t('auth.phonePlaceholder')"
/>
</div>
<div>
<label for="reg-password" class="block text-sm font-semibold text-gray-800 mb-2">
{{ t('auth.password') }}
</label>
<input
id="reg-password"
v-model="password"
type="password"
required
autocomplete="new-password"
:disabled="isSubmitting"
class="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-black focus:ring-2 focus:ring-black/5 outline-none transition-all duration-300 disabled:opacity-50 disabled:cursor-not-allowed"
:placeholder="t('auth.passwordPlaceholder')"
/>
</div>
<div>
<label for="reg-confirm-password" class="block text-sm font-semibold text-gray-800 mb-2">
{{ t('auth.confirmPassword') }}
</label>
<input
id="reg-confirm-password"
v-model="confirmPassword"
type="password"
required
autocomplete="new-password"
:disabled="isSubmitting"
class="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-black focus:ring-2 focus:ring-black/5 outline-none transition-all duration-300 disabled:opacity-50 disabled:cursor-not-allowed"
:placeholder="t('auth.confirmPasswordPlaceholder')"
/>
</div>
<!-- 错误消息 -->
<div v-if="errorMessage" class="p-3 rounded-lg bg-red-50 border border-red-200 text-red-800 text-sm">
{{ errorMessage }}
</div>
<!-- 成功消息 -->
<div v-if="successMessage" class="p-3 rounded-lg bg-green-50 border border-green-200 text-green-800 text-sm">
{{ successMessage }}
</div>
<!-- 注册按钮 -->
<button
type="submit"
:disabled="isSubmitting || !username || !email || !password || !confirmPassword"
class="w-full py-3 rounded-xl font-semibold text-white transition-all duration-300 flex items-center justify-center gap-2"
:class="isSubmitting || !username || !email || !password || !confirmPassword
? 'bg-gray-400 cursor-not-allowed'
: 'bg-black hover:bg-gray-800 shadow-lg hover:shadow-xl'"
>
<Icon v-if="isSubmitting" name="ph:circle-notch" class="w-5 h-5 animate-spin" />
<span>{{ isSubmitting ? t('auth.registering') : t('auth.registerButton') }}</span>
</button>
</form>
<!-- 返回登录链接 -->
<p class="mt-6 text-center text-sm text-gray-600">
{{ t('auth.alreadyHaveAccount') }}
<button @click="authModal.switchToLogin()" class="text-black font-semibold hover:underline">
{{ t('auth.backToLogin') }}
</button>
</p>
</div>
</div>
</div>
</Transition>
</Teleport>
</template>
<script setup lang="ts">
import { ref, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import { useUserStore } from '~/stores/user'
import { useAuthModalStore } from '~/stores/authModal'
import { useApiError } from '~/composables/useApiError'
const { t } = useI18n()
const userStore = useUserStore()
const authModal = useAuthModalStore()
const apiError = useApiError()
const username = ref('')
const email = ref('')
const phone = ref('')
const password = ref('')
const confirmPassword = ref('')
const isSubmitting = ref(false)
const errorMessage = ref('')
const successMessage = ref('')
const closeModal = () => {
if (!isSubmitting.value) {
authModal.closeAll()
}
}
// 打开时重置表单
watch(
() => authModal.isRegisterOpen,
(open) => {
if (open) {
username.value = ''
email.value = ''
phone.value = ''
password.value = ''
confirmPassword.value = ''
errorMessage.value = ''
successMessage.value = ''
}
},
)
const handleRegister = async () => {
if (isSubmitting.value) return
errorMessage.value = ''
successMessage.value = ''
if (password.value !== confirmPassword.value) {
errorMessage.value = t('auth.passwordMismatch')
return
}
if (phone.value && !/^1[3-9]\d{9}$/.test(phone.value)) {
errorMessage.value = t('auth.invalidPhone')
return
}
isSubmitting.value = true
const result = await userStore.register({
username: username.value,
email: email.value,
phone: phone.value,
password: password.value,
})
if (result.success) {
successMessage.value = t('auth.registerSuccess')
username.value = ''
email.value = ''
phone.value = ''
password.value = ''
confirmPassword.value = ''
setTimeout(() => {
authModal.closeAll()
}, 1500)
} else {
errorMessage.value = apiError({ data: { code: result.code } })
}
isSubmitting.value = false
}
// ESC 关闭
const handleKeydown = (e: KeyboardEvent) => {
if (e.key === 'Escape' && authModal.isRegisterOpen) {
closeModal()
}
}
if (typeof window !== 'undefined') {
window.addEventListener('keydown', handleKeydown)
}
</script>
<style scoped>
.modal-enter-active,
.modal-leave-active {
transition: opacity 0.3s ease;
}
.modal-enter-from,
.modal-leave-to {
opacity: 0;
}
.modal-enter-active .relative,
.modal-leave-active .relative {
transition: transform 0.3s ease, opacity 0.3s ease;
}
.modal-enter-from .relative {
transform: scale(0.95) translateY(20px);
opacity: 0;
}
.modal-leave-to .relative {
transform: scale(0.95) translateY(20px);
opacity: 0;
}
</style>