Reputation: 1745
I am developing a site using the mean stack and express-jwt to block access to API calls to my site unless a user is authenticated. There are certain API calls that I need users not logged in to access, i.e., /api/login
and /api/register
. When I access the endpoints using firebug everything seems to work as expected, it blocks under the right conditions and allows under the right conditions. I am even receiving the token under Firebug.
However, if I test using mocha/chai I am getting "401 unauthorized" error indicating "No authorization token was found". I am using the following code to ignore selected endpoints:
app.js:
let expressJwt = require('express-jwt')
app.use(expressJwt({secret: process.env.AUTH_KEY}).
unless({path: ['/api/login', '/api/register', /^\/api\/external\/.*/]}));
routes.js:
module.exports = function(app, mongoose){
app.use("/api/register", require("./routes/registration")(mongoose));
app.use("/api/external/games", require("./routes/games")(mongoose));
app.use("/api/external/shopping", require("./routes/shopping")(mongoose));
}
routes/registration.js:
'use strict'
module.exports = function(){
const express = require('express');
const router = express.Router();
const RegistrationFactory = require('../factories/RegistrationFactory');
const registrationFactory = new RegistrationFactory();
router.use(function(req, res, next) {
next();
});
/* GET users listing. */
router.post('/', function(req, res, next) {
const registrationService = registrationFactory.create();
registrationService.register(req.body, function(err, user){
if (!err && user){
console.log(err);
res.sendStatus(200);
} else {
return next(err);
}
})
});
return router;
}
I would like to block access to all of the routes except the three listed above. What am I doing wrong? I am a beginner to Node.js.
Upvotes: 1
Views: 370