Can't seem to get a login system working. I'm using a json file with the user information, and am using node.js, express and passport. Here is my code (abridged to only include the relevant information).
Index.js
const express = require('express')
const path = require('path')
const PORT = process.env.PORT || 5000
const myDB = require('./jDB');
const passport = require('passport')
, LocalStrategy = require('passport-local').Strategy;
const app = express();
const bodyParser = require('body-parser');
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser());
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(passport.initialize());
app.use(passport.session());
app.get('/success', (req, res) => res.send("Welcome "+req.query.username+"!!"));
app.get('/error', (req, res) => res.send("error logging in"));
passport.serializeUser(function(user, cb) {
cb(null, user.id);
});
passport.deserializeUser(function(id, cb) {
User.findById(id, function(err, user) {
cb(err, user);
});
});
passport.use(new LocalStrategy({
usernameField: 'username',
passwordfield : 'password'
},
function(username, password, done) {
console.log('I was given ' + username + " and " + password);
if(myDB.login(username, password)) {
console.log('Logged in');
return done(null, user);
} else {
console.log('Failed login');
return done(null, false);
}
}));
function myAuth(req, res) {
passport.authenticate('local', {
successRedirect: '/success',
failureRedirect: '/error',
})(req, res);
}
app.get('/login', (req, res) => res.render('pages/login', { root : __dirname}));
app.post('/login', (req, res) => myAuth(req, res));
app.listen(PORT, () => console.log(`Listening on ${ PORT }`));
jDB.js
const login = require('./json/users.json');
module.exports = {
login : function(mUser, mPass) {
var cryptPass = crypto.createHmac('sha256', mPass).digest('hex');
for(var exUser in login) {
console.log(json.stringify(login[exUser]) + " against " + mUser);
if(login[exUser].username == mUser && login[exUser].password == cryptPass) {
return true;
}
}
return false;
}
And the form section of login.ejs:
<form action='/login' method='post'>
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" name="username">
</div>
<div class="form-group">
<label for="pasword">Password</label>
<input type="password" class="form-control" name="password">
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
Whenever I try and log in, with the correct username and password details, I get the following error:
TypeError: next is not a function
at Strategy.strategy.error (/home/tommy/carrers-week/node_modules/passport/lib/middleware/authenticate.js:356:9)
at Strategy.authenticate (/home/tommy/carrers-week/node_modules/passport-local/lib/strategy.js:93:17)
at attempt (/home/tommy/carrers-week/node_modules/passport/lib/middleware/authenticate.js:361:16)
at authenticate (/home/tommy/carrers-week/node_modules/passport/lib/middleware/authenticate.js:362:7)
at myAuth (/home/tommy/carrers-week/index.js:195:5)
at app.post (/home/tommy/carrers-week/index.js:202:34)
at Layer.handle [as handle_request] (/home/tommy/carrers-week/node_modules/express/lib/router/layer.js:95:5)
at next (/home/tommy/carrers-week/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/tommy/carrers-week/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/tommy/carrers-week/node_modules/express/lib/router/layer.js:95:5)
I seriously have no idea how to solve this, so thanks in advance for all your help!
From the error trace one can see that this app throws within
passport.authenticate
Furthermore it grieves for a next method.
One thing i would do is scope this call within a
function next() {
console.log(arguments)
}
or just provide it:
function myAuth(req, res, next) {
passport.authenticate('local', {
successRedirect: '/success',
failureRedirect: '/error',
})(req, res, next);
}
app.post('/login', (req, res, next) => myAuth(req, res, next));
To get more insight one can look at
passport-local/blob/master/lib/strategy.js:93
try {
if (self._passReqToCallback) {
this._verify(req, username, password, verified);
} else {
this._verify(username, password, verified);
}
} catch (ex) {
return self.error(ex); //93
}
passport/blob/master/lib/middleware/authenticate.js:356
strategy.error = function(err) {
if (callback) {
return callback(err);
}
next(err); //356
};
et voila:
no callback provided to local strategy will trigger next() call
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With