UNPKG

@aimee-blue/ab-service-kit

Version:
67 lines (55 loc) 1.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.expressWithAuth = void 0; var _verifyToken = require("./verifyToken"); async function verifyTokenForRequest(allow, req) { // For safety purpose check anyway // tslint:disable-next-line if (!req || !req.headers) { return null; } const authorization = req.headers.authorization; if (!authorization) { return null; } const match = authorization.match(/^Bearer (.*)$/); if (!match) { return null; } const token = match[1]; return await (0, _verifyToken.verifyToken)({ token, allow }); } const expressWithAuth = allow => (req, res, next) => { verifyTokenForRequest(allow, req).then(result => { if (!result) { res.set('Connection', 'close'); res.status(401).json({ error: { status: 'unauthorized', message: 'Unauthorized' } }); res.end(); return; } if (result.status !== 200) { res.set('Connection', 'close'); res.status(404).json({ error: { status: 'not-found', message: 'Not found' } }); res.end(); return; } next(undefined); }).catch(err => next(err)); }; exports.expressWithAuth = expressWithAuth; //# sourceMappingURL=expressWithAuth.js.map