Skip to content

Instantly share code, notes, and snippets.

@num8er
Last active June 10, 2017 16:00
Show Gist options
  • Save num8er/404d5874c2527540025e0dce6cf6b3dd to your computer and use it in GitHub Desktop.
Save num8er/404d5874c2527540025e0dce6cf6b3dd to your computer and use it in GitHub Desktop.
Middleware for ExpressJS to catch access token and attach to request context
'use strict';
module.exports = (req, res, next) => {
const [left, right] = req.headers.authorization ? req.headers.authorization.split(' ') : [];
if(left === 'Bearer') {
req.accessToken = right;
return next();
}
req.accessToken = [
req.cookies.accessToken,
req.query.accessToken,
req.body.accessToken
].find(token => typeof token === 'string');
next();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment