Spaces:
Runtime error
Runtime error
File size: 1,072 Bytes
41af422 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import jwt from 'jsonwebtoken'
import * as dotenv from 'dotenv'
import { Status, UserRole } from '../storage/model'
import { getUserById } from '../storage/mongo'
import { getCacheConfig } from '../storage/config'
dotenv.config()
const rootAuth = async (req, res, next) => {
const config = await getCacheConfig()
if (config.siteConfig.loginEnabled) {
try {
const token = req.header('Authorization').replace('Bearer ', '')
const info = jwt.verify(token, config.siteConfig.loginSalt.trim())
req.headers.userId = info.userId
const user = await getUserById(info.userId)
if (user == null || user.status !== Status.Normal || !user.roles.includes(UserRole.Admin))
res.send({ status: 'Fail', message: '无权限 | No permission.', data: null })
else
next()
}
catch (error) {
res.send({ status: 'Unauthorized', message: error.message ?? 'Please authenticate.', data: null })
}
}
else {
res.send({ status: 'Fail', message: '无权限 | No permission.', data: null })
}
}
export { rootAuth }
|