Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: attempting to fix the req /session 500 error #419

Merged
merged 2 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,23 @@ router.post('/session', async (req, res) => {
let userInfo: { name: string; description: string; avatar: string; userId: string; root: boolean; roles: UserRole[]; config: UserConfig; advanced: AdvancedConfig }
if (userId != null) {
const user = await getUserById(userId)
if (user === null) {
globalThis.console.error(`session userId ${userId} but query user is null.`)
res.send({
status: 'Success',
message: '',
data: {
auth: hasAuth,
allowRegister,
model: config.apiModel,
title: config.siteConfig.siteTitle,
chatModels,
allChatModels: chatModelOptions,
},
})
return
}

userInfo = {
name: user.name,
description: user.description,
Expand Down
5 changes: 3 additions & 2 deletions service/src/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ async function auth(req, res, next) {
}

async function getUserId(req: Request): Promise<string | undefined> {
let token: string
try {
const token = req.header('Authorization').replace('Bearer ', '')
token = req.header('Authorization').replace('Bearer ', '')
const config = await getCacheConfig()
const info = jwt.verify(token, config.siteConfig.loginSalt.trim()) as AuthJwtPayload
return info.userId
}
catch (error) {

globalThis.console.error(`auth middleware getUserId err from token ${token} `, error.message)
}
return null
}
Expand Down
Loading