Created
February 9, 2023 12:58
-
-
Save chriswk/f56ffe05deaa1d4df01269536e5bae3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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