gancio-upstream/server/api/auth.js

45 lines
1 KiB
JavaScript
Raw Normal View History

2019-04-03 00:25:12 +02:00
const { Op } = require('sequelize')
2019-06-06 23:54:32 +02:00
const { user: User } = require('./models')
2019-04-03 00:25:12 +02:00
const Auth = {
async fillUser(req, res, next) {
2019-05-30 12:04:14 +02:00
if (!req.user) return next()
req.user = await User.findOne({
where: { id: { [Op.eq]: req.user.id }, is_active: true }
2019-06-06 23:54:32 +02:00
}).catch(e => {
res.sendStatus(404)
return next(false)
2019-04-03 00:25:12 +02:00
})
next()
2019-04-03 00:25:12 +02:00
},
async isAuth(req, res, next) {
if (!req.user) {
return res
.status(403)
.send({ message: 'Failed to authenticate token ' })
}
2019-06-08 15:16:56 +02:00
req.user = await User.findOne({
where: { id: { [Op.eq]: req.user.id }, is_active: true }
2019-04-03 00:25:12 +02:00
})
if (!req.user) {
return res
.status(403)
.send({ message: 'Failed to authenticate token ' })
}
next()
2019-04-03 00:25:12 +02:00
},
isAdmin(req, res, next) {
2019-06-06 23:54:32 +02:00
if (!req.user) {
return res
.status(403)
.send({ message: 'Failed to authenticate token ' })
}
2019-04-03 00:25:12 +02:00
if (req.user.is_admin && req.user.is_active) return next()
return res.status(403).send({ message: 'Admin needed' })
2019-06-06 23:54:32 +02:00
},
2019-04-03 00:25:12 +02:00
}
module.exports = Auth