mirror of
https://github.com/Chanzhaoyu/chatgpt-web.git
synced 2025-07-23 22:34:44 +00:00
chore: version 2.10.0
* feat: 权限验证功能 * chore: v2.10.0 * feat: 500 服务异常页面 * feat: 只有结束才会滚动到底部 * chore: 修改 CHANGELOG * chore: 不存在时输出默认报错
This commit is contained in:
19
service/src/middleware/auth.ts
Normal file
19
service/src/middleware/auth.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
const auth = async (req, res, next) => {
|
||||
const AUTH_SECRET_KEY = process.env.AUTH_SECRET_KEY
|
||||
if (typeof AUTH_SECRET_KEY === 'string' && AUTH_SECRET_KEY.length > 0) {
|
||||
try {
|
||||
const Authorization = req.header('Authorization')
|
||||
if (!Authorization || Authorization.replace('Bearer ', '').trim() !== AUTH_SECRET_KEY.trim())
|
||||
throw new Error('Error: 无访问权限 | No access rights')
|
||||
next()
|
||||
}
|
||||
catch (error) {
|
||||
res.send({ status: 'Unauthorized', message: error.message ?? 'Please authenticate.', data: null })
|
||||
}
|
||||
}
|
||||
else {
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
export { auth }
|
Reference in New Issue
Block a user