Skip to content

Commit 24fc6f3

Browse files
committed
fix: userId is string type
Signed-off-by: BobDu <i@bobdu.cc>
1 parent 509106a commit 24fc6f3

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

service/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ router.post('/session', async (req, res) => {
682682
message: '',
683683
data: {
684684
auth: hasAuth,
685+
authProxyEnabled,
685686
allowRegister,
686687
model: config.apiModel,
687688
title: config.siteConfig.siteTitle,
@@ -761,7 +762,7 @@ router.post('/user-login', authLimiter, async (req, res) => {
761762
}
762763
})
763764

764-
router.post('/user-logout', auth, async (req, res) => {
765+
router.post('/user-logout', async (req, res) => {
765766
res.send({ status: 'Success', message: '退出登录成功 | Logout successful', data: null })
766767
})
767768

service/src/middleware/auth.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ async function auth(req, res, next) {
1111
if (config.siteConfig.authProxyEnabled) {
1212
try {
1313
const username = req.header('X-Email')
14+
if (!username) {
15+
res.send({ status: 'Unauthorized', message: 'Please config auth proxy (usually is nginx) add set proxy header X-Email.', data: null })
16+
return
17+
}
1418
const user = await getUser(username)
15-
req.headers.userId = user._id
19+
req.headers.userId = user._id.toString()
1620
next()
1721
}
1822
catch (error) {
@@ -46,12 +50,7 @@ async function auth(req, res, next) {
4650
async function getUserId(req: Request): Promise<string | undefined> {
4751
let token: string
4852
try {
49-
// no Authorization info is received withput login
50-
if (!(req.header('Authorization') as string))
51-
return null // '6406d8c50aedd633885fa16f'
52-
token = req.header('Authorization').replace('Bearer ', '')
5353
const config = await getCacheConfig()
54-
5554
if (config.siteConfig.authProxyEnabled) {
5655
const username = req.header('X-Email')
5756
let user = await getUser(username)
@@ -62,6 +61,11 @@ async function getUserId(req: Request): Promise<string | undefined> {
6261
return user._id.toString()
6362
}
6463

64+
// no Authorization info is received without login
65+
if (!(req.header('Authorization') as string))
66+
return null // '6406d8c50aedd633885fa16f'
67+
token = req.header('Authorization').replace('Bearer ', '')
68+
6569
const info = jwt.verify(token, config.siteConfig.loginSalt.trim()) as AuthJwtPayload
6670
return info.userId
6771
}

0 commit comments

Comments
 (0)