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

16 lines
624 B
JavaScript
Raw Permalink 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;