@speakr/speakr-module-services
Version:
SPEAKR Shared Service Module
63 lines (55 loc) • 1.14 kB
JavaScript
;
const db = require('../../db/');
module.exports = {
byId,
byToken
};
function byId(options) {
}
function byToken(options) {
/*
* Get the token from the header
* */
let token = options.req.headers["Authorization"];
/*
* Set the filter for search
* */
let where = { jwt: token };
/*
* Get the matching token from the database
* */
return db.tokens.findOne({ where })
/*
* Handle the request
* */
.then((tokens) => {
/*
* If there is a token
* */
if(!!tokens) {
/*
* Check for datavalues
* */
tokens = tokens.dataValues ? tokens.dataValues : tokens;
/*
* Set the options for the jwt
* */
options.tokens = tokens;
token = null;
/*
* Pass to next function
* */
return Promise.resolve(options);
}
else {
/*
* You failed
* */
return Promise.reject({
code: 403,
message: "Could not persist authentication",
purpose: 'x-auth-token === null'
});
}
})
}