This commit is contained in:
Mr Swift
2024-08-11 16:11:32 +03:30
parent 21a6907d9f
commit 180cb8b5cf
18 changed files with 192 additions and 100 deletions
+6 -7
View File
@@ -25,18 +25,17 @@ const authentication = {
async isAdmin(req, res, next) {
try {
const token = req.headers?.authorization
if (!token) throw new Error('err')
if (!token) throw new Error('err1')
const decoded = jwt.verify(token.replace(/^Bearer\s/, ''), authentication.secretKey)
if (!decoded) throw new Error('err')
if (!decoded) throw new Error('err2')
const user = await User.findById(decoded._id)
if (!user || !user.scope.includes('admin')) throw new Error('err')
if (!user || !user.scope.includes('admin')) throw new Error('err3')
req.user_id = decoded._id
return next()
} catch (err) {
console.log(err)
return res401(res, 'unauthorized')
}
},
@@ -44,13 +43,13 @@ const authentication = {
async isGPS(req, res, next) {
try {
const token = req.headers?.authorization
if (!token) throw new Error('err')
if (!token) throw new Error('err1')
const decoded = jwt.verify(token.replace(/^Bearer\s/, ''), authentication.secretKey)
if (!decoded) throw new Error('err')
if (!decoded) throw new Error('err2')
const user = await User.findById(decoded._id)
if (!user || !user.scope.includes('gps')) throw new Error('err')
if (!user || !user.scope.includes('gps')) throw new Error('err3')
req.user_id = decoded._id
return next()