UNPKG

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