Unless is not working
iamchathu opened this issue ยท 9 comments
I have a route which is /authenticate
I have defined it as
app.use(expressjwt({secret: config.sessionSecret}).unless({path: ['/authenticate']}));
But when I do a post to /authenticate
path it give me an error saying
UnauthorizedError: No authorization token was found
...
/authenticate
is working without express-jwt
can you send me a complete 1 file example reproducing the issue?
I am using this feature in other projects.
'use strict';
const path = require('path');
const express = require('express');
const mongoose = require('mongoose');
const helmet = require('helmet');
const bodyParser = require('body-parser');
const chalk = require('chalk');
const nunjucks = require('nunjucks');
const expressjwt = require('express-jwt');
const config = require('./config/config');
// Init Express App
const app = express();
// Connect to database (Open sesame)
mongoose.Promise = global.Promise;
const db = mongoose.connect(config.db.uri, config.db.options, function(err) {
// Log Error
if (err) {
console.error(chalk.red('Could not connect to MongoDB!'));
console.log(err);
} else {
// Enabling mongoose debug mode if required
mongoose.set('debug', config.db.debug);
}
});
// Use helmet to secure Express headers
const SIX_MONTHS = 15778476000;
app.use(helmet.frameguard());
app.use(helmet.xssFilter());
app.use(helmet.noSniff());
app.use(helmet.ieNoOpen());
app.use(helmet.hsts({maxAge: SIX_MONTHS, includeSubdomains: true, force: true}));
app.disable('x-powered-by');
// Enable body parsing
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
// Configure Templating Engine
nunjucks.configure('views', {
autoescape: true,
express: app
});
//authenticate
app.use(expressjwt({secret: config.sessionSecret}).unless({path: ['/authenticate']}));
//models
const adminModel = require('./modules/admin/models/admin.model');
// routes
const adminRoutes = require('./modules/admin/routes/admin.routes');
const defaultRoutes = require('./modules/core/server/routes/default.routes');
app.use('/', adminRoutes);
app.use('/', defaultRoutes);
express.static(path.join(__dirname, 'public'));
// Start the app by listening on <port> at <host>
app.listen(config.port, config.host, function() {
// Create server URL
var server = (process.env.NODE_ENV === 'secure'
? 'https://'
: 'http://') + config.host + ':' + config.port;
// Logging initialization
console.log('--');
console.log(chalk.green(config.app.title));
console.log();
console.log(chalk.yellow('Environment: ' + process.env.NODE_ENV));
console.log(chalk.green('Server: ' + server));
console.log(chalk.green('Database: ' + config.db.uri));
console.log('--');
});
@iamchathu did you ever figure out the problem with it?
Not yet. :(
I had same problem. Try setting strict: false in your configuration object
@iamchathu
/authenticate
will redirect to /authenticate/
try add slash to end of path
change to
app.use(expressjwt({secret: config.sessionSecret}).unless({path: ['/authenticate/']}));
same prob
Same problem
const unless = require("express-unless")
function checkAuth(req, res, next) {
if (req.session.userName) {
if (req.session.userName) res.locals.userName = req.session.userName
return next()
}
res.status(200).json({ message: "NOT_AUTHENTICATED" })
}
checkAuth.unless = unless
app.use(checkAuth.unless({ path: ["/auth/login"] }))
Error:
TypeError: checkAuth.unless is not a function
Reverting to 1.0.0
I'll close this issue as it is 4 years old and it is a mix of different issues.
Please feel free to create new issues with specific code and I will try to help