UNPKG

1.86 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.parseAuthentication = parseAuthentication;
4exports.authenticate = authenticate;
5const commons_1 = require("@feathersjs/commons");
6const authentication_1 = require("@feathersjs/authentication");
7const debug = (0, commons_1.createDebug)('@feathersjs/express/authentication');
8const toHandler = (func) => {
9 return (req, res, next) => func(req, res, next).catch((error) => next(error));
10};
11function parseAuthentication(settings = {}) {
12 return toHandler(async (req, res, next) => {
13 var _a;
14 const app = req.app;
15 const service = (_a = app.defaultAuthentication) === null || _a === void 0 ? void 0 : _a.call(app, settings.service);
16 if (!service) {
17 return next();
18 }
19 const config = service.configuration;
20 const authStrategies = settings.strategies || config.parseStrategies || config.authStrategies || [];
21 if (authStrategies.length === 0) {
22 debug('No `authStrategies` or `parseStrategies` found in authentication configuration');
23 return next();
24 }
25 const authentication = await service.parse(req, res, ...authStrategies);
26 if (authentication) {
27 debug('Parsed authentication from HTTP header', authentication);
28 req.feathers = { ...req.feathers, authentication };
29 }
30 return next();
31 });
32}
33function authenticate(settings, ...strategies) {
34 const hook = (0, authentication_1.authenticate)(settings, ...strategies);
35 return toHandler(async (req, _res, next) => {
36 const app = req.app;
37 const params = req.feathers;
38 const context = { app, params };
39 await hook(context);
40 req.feathers = context.params;
41 return next();
42 });
43}
44//# sourceMappingURL=authentication.js.map
\No newline at end of file