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
+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>
|
||||
|
||||
Reference in new issue
Block a user