feat: add control plane authentication baseline
This commit is contained in:
1 parent
a4c93ff1a7
commit
0d10e2d124
21 files changed
+1403
-18
No files matched your search
@@ -0,0 +1,3 @@
|
||||
VITE_CONTROL_PLANE_URL=http://127.0.0.1:8787
|
||||
# 仅用于视觉原型预览;接入 D3 认证服务时保持 remote。
|
||||
VITE_AUTH_MODE=remote
|
||||
+28
-6
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue'
|
||||
import { computed, onMounted, ref } from 'vue'
|
||||
import {
|
||||
ArrowLeftRight,
|
||||
Bell,
|
||||
@@ -18,6 +18,7 @@ import AuthView from './views/AuthView.vue'
|
||||
import AdminConsole from './views/AdminConsole.vue'
|
||||
import UserConsole from './views/UserConsole.vue'
|
||||
import type { Portal } from './types'
|
||||
import { authClient, type AuthUser } from './services/auth'
|
||||
|
||||
const portal = ref<Portal>('user')
|
||||
const activeView = ref('overview')
|
||||
@@ -25,9 +26,23 @@ const sidebarCollapsed = ref(false)
|
||||
const mobileNavigationOpen = ref(false)
|
||||
const toastMessage = ref('')
|
||||
const authenticated = ref(false)
|
||||
const authUser = ref<AuthUser>()
|
||||
const accountMenuOpen = ref(false)
|
||||
const userName = ref('林默')
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
const user = await authClient.currentUser()
|
||||
if (user) {
|
||||
authUser.value = user
|
||||
userName.value = user.displayName
|
||||
authenticated.value = true
|
||||
}
|
||||
} catch {
|
||||
notify('认证服务暂不可用,请稍后重试')
|
||||
}
|
||||
})
|
||||
|
||||
const navigation = computed(() => portal.value === 'user' ? userNavigation : adminNavigation)
|
||||
const activeLabel = computed(() => navigation.value.flatMap((group) => group.items).find((item) => item.id === activeView.value)?.label ?? '总览')
|
||||
|
||||
@@ -39,6 +54,10 @@ function navigate(view: string) {
|
||||
}
|
||||
|
||||
function switchPortal() {
|
||||
if (authUser.value?.role !== 'admin') {
|
||||
notify('当前账号没有管理端权限')
|
||||
return
|
||||
}
|
||||
portal.value = portal.value === 'user' ? 'admin' : 'user'
|
||||
activeView.value = portal.value === 'user' ? 'overview' : 'admin-overview'
|
||||
mobileNavigationOpen.value = false
|
||||
@@ -52,8 +71,9 @@ function notify(message: string) {
|
||||
}, 2800)
|
||||
}
|
||||
|
||||
function signIn(name: string) {
|
||||
userName.value = name
|
||||
function signIn(user: AuthUser) {
|
||||
userName.value = user.displayName
|
||||
authUser.value = user
|
||||
authenticated.value = true
|
||||
activeView.value = 'overview'
|
||||
notify('登录成功,欢迎进入模亦灵枢')
|
||||
@@ -64,6 +84,8 @@ function logout() {
|
||||
portal.value = 'user'
|
||||
activeView.value = 'overview'
|
||||
authenticated.value = false
|
||||
authUser.value = undefined
|
||||
void authClient.logout().catch(() => undefined)
|
||||
notify('已安全退出登录')
|
||||
}
|
||||
|
||||
@@ -133,11 +155,11 @@ function openAccountSettings() {
|
||||
</div>
|
||||
<div class="topbar-actions">
|
||||
<button class="global-search desktop-only" @click="notify('全局搜索将在接入真实数据后开放')"><Search :size="16" /><span>搜索模型、用户或任务</span><kbd>⌘ K</kbd></button>
|
||||
<button class="portal-switch" @click="switchPortal"><ArrowLeftRight :size="16" /><span>{{ portal === 'admin' ? '返回用户端' : '进入管理端' }}</span></button>
|
||||
<button v-if="authUser?.role === 'admin'" class="portal-switch" @click="switchPortal"><ArrowLeftRight :size="16" /><span>{{ portal === 'admin' ? '返回用户端' : '进入管理端' }}</span></button>
|
||||
<button class="icon-button desktop-only" aria-label="通知" @click="notify('当前没有新的通知')"><Bell :size="18" /></button>
|
||||
<div class="account-menu-wrap">
|
||||
<button class="user-menu" :class="{ active: accountMenuOpen }" @click="accountMenuOpen = !accountMenuOpen"><span>{{ userName.slice(0, 1) }}</span><div><strong>{{ userName }}</strong><small>{{ portal === 'admin' ? '平台管理员' : '管理员账号' }}</small></div></button>
|
||||
<Transition name="menu-pop"><div v-if="accountMenuOpen" class="account-popover"><header><span>{{ userName.slice(0, 1) }}</span><div><strong>{{ userName }}</strong><small>demo@moyi.ai</small></div></header><button @click="openAccountSettings"><Settings :size="16" />账号设置</button><button class="logout-button" @click="logout"><LogOut :size="16" />退出登录</button></div></Transition>
|
||||
<button class="user-menu" :class="{ active: accountMenuOpen }" @click="accountMenuOpen = !accountMenuOpen"><span>{{ userName.slice(0, 1) }}</span><div><strong>{{ userName }}</strong><small>{{ authUser?.role === 'admin' ? '平台管理员' : '普通用户' }}</small></div></button>
|
||||
<Transition name="menu-pop"><div v-if="accountMenuOpen" class="account-popover"><header><span>{{ userName.slice(0, 1) }}</span><div><strong>{{ userName }}</strong><small>{{ authUser?.account }}</small></div></header><button @click="openAccountSettings"><Settings :size="16" />账号设置</button><button class="logout-button" @click="logout"><LogOut :size="16" />退出登录</button></div></Transition>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
export type AuthRole = 'user' | 'admin'
|
||||
|
||||
export interface AuthUser {
|
||||
id: string
|
||||
account: string
|
||||
accountKind: 'email' | 'phone'
|
||||
displayName: string
|
||||
role: AuthRole
|
||||
status: 'active' | 'disabled'
|
||||
createdAt: string
|
||||
lastLoginAt?: string
|
||||
}
|
||||
|
||||
export interface RegisterInput {
|
||||
invitationCode: string
|
||||
account: string
|
||||
password: string
|
||||
displayName?: string
|
||||
}
|
||||
|
||||
export interface LoginInput {
|
||||
account: string
|
||||
password: string
|
||||
}
|
||||
|
||||
export interface AuthClient {
|
||||
login(input: LoginInput): Promise<AuthUser>
|
||||
register(input: RegisterInput): Promise<AuthUser>
|
||||
currentUser(): Promise<AuthUser | undefined>
|
||||
logout(): Promise<void>
|
||||
}
|
||||
|
||||
class HttpAuthClient implements AuthClient {
|
||||
constructor(private readonly baseUrl: string) {}
|
||||
|
||||
async login(input: LoginInput) {
|
||||
const payload = await this.request<{ user: AuthUser }>('/api/auth/login', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(input),
|
||||
})
|
||||
return payload.user
|
||||
}
|
||||
|
||||
async register(input: RegisterInput): Promise<AuthUser> {
|
||||
const payload = await this.request<{ user: AuthUser }>('/api/auth/register', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(input),
|
||||
})
|
||||
return payload.user
|
||||
}
|
||||
|
||||
async currentUser() {
|
||||
try {
|
||||
const payload = await this.request<{ user: AuthUser }>('/api/auth/me')
|
||||
return payload.user
|
||||
} catch (error) {
|
||||
if (error instanceof AuthApiError && error.status === 401) return undefined
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
async logout() {
|
||||
await this.request('/api/auth/logout', { method: 'POST' })
|
||||
}
|
||||
|
||||
private async request<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
let response: Response
|
||||
try {
|
||||
response = await fetch(`${this.baseUrl}${path}`, {
|
||||
...init,
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...init?.headers,
|
||||
},
|
||||
})
|
||||
} catch {
|
||||
throw new AuthApiError(0, '无法连接 Control Plane,请确认认证服务已启动。')
|
||||
}
|
||||
|
||||
if (response.status === 204) return undefined as T
|
||||
const payload = await response.json().catch(() => ({})) as { error?: { code?: string; message?: string } }
|
||||
if (!response.ok) throw new AuthApiError(response.status, payload.error?.message || '认证请求失败', payload.error?.code)
|
||||
return payload as T
|
||||
}
|
||||
}
|
||||
|
||||
class DemoAuthClient implements AuthClient {
|
||||
private readonly sessionKey = 'moyi_demo_session'
|
||||
private readonly user: AuthUser = {
|
||||
id: 'demo_admin',
|
||||
account: 'demo@moyi.ai',
|
||||
accountKind: 'email',
|
||||
displayName: '林默',
|
||||
role: 'admin',
|
||||
status: 'active',
|
||||
createdAt: '2026-08-20T00:00:00.000Z',
|
||||
}
|
||||
|
||||
async login(input: LoginInput) {
|
||||
if (!input.account.trim() || !input.password) throw new AuthApiError(400, '请输入账号和密码')
|
||||
sessionStorage.setItem(this.sessionKey, '1')
|
||||
return this.user
|
||||
}
|
||||
|
||||
async register(input: RegisterInput): Promise<AuthUser> {
|
||||
if (!input.invitationCode.trim()) throw new AuthApiError(400, '第一版内测需要填写邀请码')
|
||||
if (input.password.length < 8) throw new AuthApiError(400, '密码至少需要 8 位')
|
||||
sessionStorage.setItem(this.sessionKey, '1')
|
||||
return { ...this.user, account: input.account, displayName: input.displayName || input.account.split('@')[0] || 'MOYI 用户', role: 'user' }
|
||||
}
|
||||
|
||||
async currentUser() {
|
||||
return sessionStorage.getItem(this.sessionKey) ? this.user : undefined
|
||||
}
|
||||
|
||||
async logout() {
|
||||
sessionStorage.removeItem(this.sessionKey)
|
||||
}
|
||||
}
|
||||
|
||||
export class AuthApiError extends Error {
|
||||
constructor(
|
||||
readonly status: number,
|
||||
message: string,
|
||||
readonly code?: string,
|
||||
) {
|
||||
super(message)
|
||||
}
|
||||
}
|
||||
|
||||
const authMode = import.meta.env.VITE_AUTH_MODE || 'remote'
|
||||
const controlPlaneUrl = (import.meta.env.VITE_CONTROL_PLANE_URL || 'http://127.0.0.1:8787').replace(/\/$/, '')
|
||||
|
||||
export const authClient: AuthClient = authMode === 'demo'
|
||||
? new DemoAuthClient()
|
||||
: new HttpAuthClient(controlPlaneUrl)
|
||||
@@ -16,12 +16,13 @@ import {
|
||||
Sparkles,
|
||||
Zap,
|
||||
} from 'lucide-vue-next'
|
||||
import { AuthApiError, authClient, type AuthUser } from '../services/auth'
|
||||
|
||||
type AuthMode = 'login' | 'register' | 'forgot'
|
||||
type RegisterChannel = 'email' | 'phone'
|
||||
|
||||
const emit = defineEmits<{
|
||||
success: [name: string]
|
||||
success: [user: AuthUser]
|
||||
notify: [message: string]
|
||||
}>()
|
||||
|
||||
@@ -61,28 +62,39 @@ function sendCode() {
|
||||
emit('notify', '验证码已发送,原型阶段可输入任意 6 位数字')
|
||||
}
|
||||
|
||||
function submitLogin() {
|
||||
async function submitLogin() {
|
||||
if (!account.value.trim() || !password.value) return emit('notify', '请输入账号和密码')
|
||||
loading.value = true
|
||||
window.setTimeout(() => {
|
||||
try {
|
||||
const user = await authClient.login({ account: account.value, password: password.value })
|
||||
loading.value = false
|
||||
emit('success', '林默')
|
||||
}, 650)
|
||||
emit('success', user)
|
||||
} catch (error) {
|
||||
loading.value = false
|
||||
emit('notify', error instanceof AuthApiError ? error.message : '登录失败,请稍后重试')
|
||||
}
|
||||
}
|
||||
|
||||
function submitRegister() {
|
||||
async function submitRegister() {
|
||||
if (!invitationCode.value.trim()) return emit('notify', '第一版内测需要填写邀请码')
|
||||
if (!account.value.trim()) return emit('notify', `请输入${accountLabel.value}`)
|
||||
if (verificationCode.value.trim().length < 4) return emit('notify', '请输入收到的验证码')
|
||||
if (password.value.length < 8) return emit('notify', '密码至少需要 8 位')
|
||||
if (password.value !== confirmPassword.value) return emit('notify', '两次输入的密码不一致')
|
||||
if (!agreeTerms.value) return emit('notify', '请先同意服务协议与隐私政策')
|
||||
loading.value = true
|
||||
window.setTimeout(() => {
|
||||
try {
|
||||
const user = await authClient.register({
|
||||
invitationCode: invitationCode.value,
|
||||
account: account.value,
|
||||
password: password.value,
|
||||
})
|
||||
loading.value = false
|
||||
switchMode('login')
|
||||
emit('notify', '注册成功,请使用新账号登录')
|
||||
}, 700)
|
||||
emit('notify', '注册成功,欢迎 ' + user.displayName + '。请使用新账号登录')
|
||||
} catch (error) {
|
||||
loading.value = false
|
||||
emit('notify', error instanceof AuthApiError ? error.message : '注册失败,请稍后重试')
|
||||
}
|
||||
}
|
||||
|
||||
function submitReset() {
|
||||
|
||||
Reference in new issue
Block a user