mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-22 01:02:05 -07:00
50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
/*
|
|
This is a middleware library for handling and processing JWT tokens.
|
|
|
|
This middleware inspects the request header for a JWT token.
|
|
If found, will populate req.locals.jwtToken with the JWT token.
|
|
*/
|
|
|
|
'use strict'
|
|
|
|
// This function searches the header for the a JWT token in the authorization header.
|
|
// If one is found, this middleware passes the JWT token through the
|
|
// req.locals.jwtToken property.
|
|
const getTokenFromHeaders = (req, res, next) => {
|
|
try {
|
|
// Only executes if the authorization header exists.
|
|
if (req.headers.authorization) {
|
|
// Retrieve the auth string from the header object.
|
|
const authStr = req.headers.authorization
|
|
|
|
// If the header is proceeded by the word 'Token'
|
|
if (authStr.split(' ')[0] === 'Token') {
|
|
const token = authStr.split(' ')[1]
|
|
|
|
// console.log(`JWT found: ${token}`)
|
|
|
|
// Create the req.locals property if it does not yet exist.
|
|
if (!req.locals) {
|
|
req.locals = {
|
|
jwtToken: token,
|
|
proLimit: false,
|
|
apiLevel: 0
|
|
}
|
|
} else {
|
|
req.locals.jwtToken = token
|
|
}
|
|
}
|
|
}
|
|
} catch (err) {
|
|
console.log('Error in getTokenFromHeaders: ', err)
|
|
}
|
|
|
|
next()
|
|
}
|
|
|
|
const jwtAuth = {
|
|
getTokenFromHeaders
|
|
}
|
|
|
|
module.exports = jwtAuth
|