Skip to content

Commit

Permalink
fix: sync userinfo on multiple open browser (Close Chanzhaoyu#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerwin committed Jul 13, 2023
1 parent d067727 commit cf88467
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
13 changes: 12 additions & 1 deletion service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 }[] = []
Expand Down Expand Up @@ -596,6 +606,7 @@ router.post('/session', async (req, res) => {
title: config.siteConfig.siteTitle,
chatModels,
allChatModels: chatModelOptions,
userInfo,
},
})
}
Expand Down
4 changes: 4 additions & 0 deletions src/router/permission.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/store/modules/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit cf88467

Please sign in to comment.