taapi-cache
Version:
TAAPI.IO Cache Package. A convenient way to fetch candles, store them and reuse them.
53 lines (43 loc) • 1.4 kB
JavaScript
/**
* This middleware class adds more information to the request
* object. Mainly information about the authentication status
* of the querier
*/
// Import the JSON Web Token package
const config = require("../config/config");
module.exports = async (req, res, next) => {
// Get the authentication header
const authHeader = req.get("Authorization");
req.isAuthenticated = false;
/**
* Add the response object to the request object.
* This is used by the schema resolv functions to
* set apropriate response status codes
*/
req.response = res;
/**
* Authentication depends on which request
* method is used. This can be GET or POST
*/
const requestMethod = req.method.toLowerCase();
let providedToken = null; // Fetch secret from GET Request
if (requestMethod === "get" || requestMethod === "delete") {
if (!req.query.secret) {
req.isAuthenticated = false;
return next();
} // Store the users auth token
providedToken = req.query.secret;
} else if (requestMethod === "post") {
// Fetch secret from POST Request
if (!req.body.secret) {
req.isAuthenticated = false;
return next();
} // Store the users auth token
providedToken = req.body.secret;
}
if (providedToken && config.auth.secret === providedToken) {
req.isAuthenticated = true;
return next();
}
return next();
};