Last active
June 10, 2017 16:00
-
-
Save num8er/404d5874c2527540025e0dce6cf6b3dd to your computer and use it in GitHub Desktop.
Middleware for ExpressJS to catch access token and attach to request context
This file contains hidden or 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
'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