Files
arcticnewone/app/utils/casdoorClient.ts
T
virtheart 337eb4f05a 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 单测
2026-08-09 00:18:47 +08:00

48 lines
1.7 KiB
TypeScript

import { useRuntimeConfig } from '#app'
/**
* Casdoor 客户端配置读取(public runtimeConfig 注入,不含任何 secret)。
* 登录跳转走后端端点 /api/auth/casdoor-login,这里仅提供注册页与单点登出地址。
*/
export interface CasdoorPublic {
serverUrl: string
clientId: string
orgName: string
appName: string
redirectPath: string
}
export function getCasdoorPublic(): CasdoorPublic {
const c = useRuntimeConfig()
const pub = (c.public ?? {}) as Record<string, string | undefined>
return {
serverUrl: (pub.casdoorServerUrl as string) || '',
clientId: (pub.casdoorClientId as string) || '',
orgName: (pub.casdoorOrgName as string) || '',
appName: (pub.casdoorAppName as string) || '',
redirectPath: (pub.casdoorRedirectPath as string) || '/api/auth/callback',
}
}
/** 注册账号统一在 Casdoor 完成 */
export function getCasdoorSignupUrl(): string {
const { serverUrl, appName } = getCasdoorPublic()
if (!serverUrl || !appName) return ''
return `${serverUrl}/signup/${appName}`
}
/** 本站回调完整地址(用于登出回跳) */
export function getCasdoorRedirectUri(): string {
const { redirectPath } = getCasdoorPublic()
const { public: p } = useRuntimeConfig()
const siteUrl = String((p as any).siteUrl || '').replace(/\/$/, '')
return `${siteUrl}${redirectPath}`
}
/** 单点登出地址(可选使用:登出后跳回本站回调) */
export function getCasdoorLogoutUrl(): string {
const { serverUrl, clientId } = getCasdoorPublic()
if (!serverUrl || !clientId) return ''
return `${serverUrl}/logout?client_id=${clientId}&redirect_uri=${encodeURIComponent(getCasdoorRedirectUri())}`
}