1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/src/lib/middleware/api-token-middleware.ts
Christopher Kolstad 240c6a77a1
Feat/options need types (#794)
feat: options are now typed

- This makes it easier to know what to send to unleash.start / unleash.create
- Using a Partial to instantiate the config, then melding it with defaults to get a config object with all fields set either to their defaults or to whatever is passed in.


Co-authored-by: Fredrik Strand Oseberg <fredrik.no@gmail.com>
Co-authored-by: Ivar Conradi Østhus <ivarconr@gmail.com>
2021-04-22 10:07:10 +02:00

39 lines
991 B
TypeScript

/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { IUnleashConfig } from '../types/option';
const apiAccessMiddleware = (
{
getLogger,
authentication,
}: Pick<IUnleashConfig, 'getLogger' | 'authentication'>,
{ apiTokenService }: any,
): any => {
const logger = getLogger('/middleware/api-token.ts');
logger.info('Enabling api-token middleware');
if (!authentication.enableApiToken) {
return (req, res, next) => next();
}
return (req, res, next) => {
if (req.user) {
return next();
}
try {
const userToken = req.header('authorization');
const user = apiTokenService.getUserForToken(userToken);
if (user) {
req.user = user;
}
} catch (error) {
logger.error(error);
}
return next();
};
};
module.exports = apiAccessMiddleware;
export default apiAccessMiddleware;