Skip to content

Commit cf88467

Browse files
author
Kerwin
committed
fix: sync userinfo on multiple open browser (Close #296)
1 parent d067727 commit cf88467

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

service/src/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { ChatMessage } from './chatgpt'
88
import { abortChatProcess, chatConfig, chatReplyProcess, containsSensitiveWords, initAuditService } from './chatgpt'
99
import { auth, getUserId } from './middleware/auth'
1010
import { clearApiKeyCache, clearConfigCache, getApiKeys, getCacheApiKeys, getCacheConfig, getOriginConfig } from './storage/config'
11-
import type { AuditConfig, CHATMODEL, ChatInfo, ChatOptions, Config, KeyConfig, MailConfig, SiteConfig, UserInfo } from './storage/model'
11+
import type { AuditConfig, CHATMODEL, ChatInfo, ChatOptions, Config, KeyConfig, MailConfig, SiteConfig, UserConfig, UserInfo } from './storage/model'
1212
import { Status, UsageResponse, UserRole, chatModelOptions } from './storage/model'
1313
import {
1414
clearChat,
@@ -557,8 +557,18 @@ router.post('/session', async (req, res) => {
557557
key: string
558558
value: string
559559
}[] = []
560+
let userInfo: { name: string; description: string; avatar: string; userId: string; root: boolean; config: UserConfig }
560561
if (userId != null) {
561562
const user = await getUserById(userId)
563+
userInfo = {
564+
name: user.name,
565+
description: user.description,
566+
avatar: user.avatar,
567+
userId: user._id.toString(),
568+
root: user.roles.includes(UserRole.Admin),
569+
config: user.config,
570+
}
571+
562572
const keys = (await getCacheApiKeys()).filter(d => hasAnyRole(d.userRoles, user.roles))
563573

564574
const count: { key: string; count: number }[] = []
@@ -596,6 +606,7 @@ router.post('/session', async (req, res) => {
596606
title: config.siteConfig.siteTitle,
597607
chatModels,
598608
allChatModels: chatModelOptions,
609+
userInfo,
599610
},
600611
})
601612
}

src/router/permission.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Router } from 'vue-router'
22
import { useAuthStoreWithout } from '@/store/modules/auth'
3+
import { useUserStore } from '@/store'
34

45
export function setupPageGuard(router: Router) {
56
router.beforeEach(async (to, from, next) => {
@@ -9,6 +10,9 @@ export function setupPageGuard(router: Router) {
910
const data = await authStore.getSession()
1011
if (String(data.auth) === 'false' && authStore.token)
1112
await authStore.removeToken()
13+
else
14+
await useUserStore().updateUserInfo(false, data.userInfo)
15+
1216
if (to.path === '/500')
1317
next({ name: 'Root' })
1418
else

src/store/modules/auth/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ interface SessionResponse {
2121
key: string
2222
value: string
2323
}[]
24+
userInfo: { name: string; description: string; avatar: string; userId: string; root: boolean; config: UserConfig }
2425
}
2526

2627
export interface AuthState {

0 commit comments

Comments
 (0)