From cf88467f4bfc193f80e3abb9ec7882408d937a09 Mon Sep 17 00:00:00 2001 From: Kerwin Date: Thu, 13 Jul 2023 21:04:05 +0800 Subject: [PATCH] fix: sync userinfo on multiple open browser (Close #296) --- service/src/index.ts | 13 ++++++++++++- src/router/permission.ts | 4 ++++ src/store/modules/auth/index.ts | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/service/src/index.ts b/service/src/index.ts index f437c72f12..bd304e93d5 100644 --- a/service/src/index.ts +++ b/service/src/index.ts @@ -8,7 +8,7 @@ import type { ChatMessage } from './chatgpt' import { abortChatProcess, chatConfig, chatReplyProcess, containsSensitiveWords, initAuditService } from './chatgpt' import { auth, getUserId } from './middleware/auth' import { clearApiKeyCache, clearConfigCache, getApiKeys, getCacheApiKeys, getCacheConfig, getOriginConfig } from './storage/config' -import type { AuditConfig, CHATMODEL, ChatInfo, ChatOptions, Config, KeyConfig, MailConfig, SiteConfig, UserInfo } from './storage/model' +import type { AuditConfig, CHATMODEL, ChatInfo, ChatOptions, Config, KeyConfig, MailConfig, SiteConfig, UserConfig, UserInfo } from './storage/model' import { Status, UsageResponse, UserRole, chatModelOptions } from './storage/model' import { clearChat, @@ -557,8 +557,18 @@ router.post('/session', async (req, res) => { key: string value: string }[] = [] + let userInfo: { name: string; description: string; avatar: string; userId: string; root: boolean; config: UserConfig } if (userId != null) { const user = await getUserById(userId) + userInfo = { + name: user.name, + description: user.description, + avatar: user.avatar, + userId: user._id.toString(), + root: user.roles.includes(UserRole.Admin), + config: user.config, + } + const keys = (await getCacheApiKeys()).filter(d => hasAnyRole(d.userRoles, user.roles)) const count: { key: string; count: number }[] = [] @@ -596,6 +606,7 @@ router.post('/session', async (req, res) => { title: config.siteConfig.siteTitle, chatModels, allChatModels: chatModelOptions, + userInfo, }, }) } diff --git a/src/router/permission.ts b/src/router/permission.ts index b923aa7d81..5cfc2bdf7c 100644 --- a/src/router/permission.ts +++ b/src/router/permission.ts @@ -1,5 +1,6 @@ import type { Router } from 'vue-router' import { useAuthStoreWithout } from '@/store/modules/auth' +import { useUserStore } from '@/store' export function setupPageGuard(router: Router) { router.beforeEach(async (to, from, next) => { @@ -9,6 +10,9 @@ export function setupPageGuard(router: Router) { const data = await authStore.getSession() if (String(data.auth) === 'false' && authStore.token) await authStore.removeToken() + else + await useUserStore().updateUserInfo(false, data.userInfo) + if (to.path === '/500') next({ name: 'Root' }) else diff --git a/src/store/modules/auth/index.ts b/src/store/modules/auth/index.ts index 1186f7a394..66f6b8ab00 100644 --- a/src/store/modules/auth/index.ts +++ b/src/store/modules/auth/index.ts @@ -21,6 +21,7 @@ interface SessionResponse { key: string value: string }[] + userInfo: { name: string; description: string; avatar: string; userId: string; root: boolean; config: UserConfig } } export interface AuthState {