1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-12-04 19:32:20 -05:00
shimapan/app/util/auth/authenticateRequest.js

16 lines
624 B
JavaScript
Raw Normal View History

2019-01-02 16:47:18 -05:00
const config = require('config');
const authenticate = require('./authenticate');
const rateLimit = require('../rateLimit');
const authenticateRequest = scope => (req, res, next) => {
rateLimit(config.get('RateLimit.api.window'), config.get('RateLimit.api.max'))(req, res, async () => {
const status = await authenticate(req, scope);
if (status.authenticated) {
if (status.permission) {
next();
} else res.status(403).json({message: 'Forbidden.'});
} else res.status(401).json({message: 'Unauthorized.'});
});
};
module.exports = authenticateRequest;