Skip to content

Commit

Permalink
warning being printed if decodeToken is not bootstrapped
Browse files Browse the repository at this point in the history
  • Loading branch information
afontainec committed Jul 5, 2020
1 parent 9e29a6a commit fdd11e2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
3 changes: 2 additions & 1 deletion models/middleware/accessToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ const decode = async (req, res, next) => {
if (!hasExpired(decoded)) await addIsAuthenticated(req, decoded);
return next();
} catch (error) {
if (error.chinchayCode === 'middlewareMissingTheWall') console.log(`WARNING: ${error.message} req.isAuthenticatedByToken() will return false.`);
return next();
}
};

const addIsAuthenticated = async (req, decoded) => {
if (!req || !decoded) return;
if (!TheWall) throw new ChinchayError(new Error('TheWall should be defined in chainfile and middleware bootstraped.'), 'middlewareMissingTheWall');
if (!TheWall) throw new ChinchayError(new Error('TheWall should be defined in chainfile and accessToken bootstraped.'), 'middlewareMissingTheWall');
req.isAuthenticatedByToken = () => {
return true;
};
Expand Down
5 changes: 3 additions & 2 deletions test/models/middleware/accessToken/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ describe('Middleware: accessToken: decode', () => { // eslint-disable-line

it('TheWall is not bootstrapped', (done) => { // eslint-disable-line
const req = Req.generate();
req.user_id = 44;
const token = jwt.sign({ user: 1, exp: TOMORROW }, secret);
req.headers.Authorization = `Bearer ${token}`;
accessToken.unbootstrap();
accessToken.decode(req, null, () => {
assert.isUndefined(req.user);
Expand Down Expand Up @@ -78,7 +79,7 @@ describe('Middleware: accessToken: decode', () => { // eslint-disable-line
const req = Req.generate();
const token = jwt.sign({ user: 1, exp: TOMORROW }, secret);
req.headers.Authorization = `Bearer ${token}`;
delete req.isAuthenticated;
delete req.isAuthenticatedByToken;
accessToken.decode(req, null, () => {
assert.equal(req.user.id, 1);
assert.equal(req.user.access.length, 1);
Expand Down

0 comments on commit fdd11e2

Please sign in to comment.