Skip to content

Instantly share code, notes, and snippets.

@chriswk
Created February 9, 2023 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriswk/f56ffe05deaa1d4df01269536e5bae3e to your computer and use it in GitHub Desktop.
Save chriswk/f56ffe05deaa1d4df01269536e5bae3e to your computer and use it in GitHub Desktop.
export const tokenMiddleware = (tokenValidator: TokenValidator) => {
return async (req: Request, res: Response, next: NextFunction) => {
const apiToken = req.header('authorization');
if (!apiToken) {
return res
.status(401)
.send({ message: 'You must authenticate to use Unleash on the Edge' });
}
await tokenValidator.registerToken(apiToken);
if (!(await tokenValidator.isValid(apiToken))) {
return res.status(401).send({
message: 'The provided token is invalid.',
});
}
req.token = apiToken;
return next();
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment