javascript - NodeJS Passport Authentication - always redirected to failure url -


i redirected failure url irrespective of login details, nothing logged on console , showing no errors. ideas why happening?

login jade

extends layout  block content   h1 login   p please login below   form(method='post', action='/users/login',enctype='multipart/form-data')     .form-group       label username       input.form-control(name='username', type='text', placeholder='enter username')     .form-group       label password       input.form-control(name='password', type='password', placeholder='enter password')     input.btn.btn-default(name='submit', type='submit', value='login') 

user.js

var express = require('express'); var router = express.router(); var multer = require('multer'); var uploads =  multer({ dest: './uploads' }); var passport = require('passport'); var localstrategy = require('passport-local').strategy;  var user = require('../models/usermodel');  /* users listing. */ router.get('/', function(req, res, next) {   res.send('respond resource'); });  router.get('/register', function(req, res, next) {   res.render('register',{ title: 'register'}) });  router.get('/login', function(req, res, next) {   res.render('login',{ title: 'login'}) });  passport.serializeuser(function(user, done) {   done(null, user.id); });  passport.deserializeuser(function(id, done) {   user.getuserbyid(id, function(err, user) {     done(err, user);   }); });  passport.use(new localstrategy(   function(username, password, done){     user.getuserbyusername(username, function(err, user){       if(err) throw err;       if(!user){         console.log('unknown user');         return done(null, false,{message:'unkown user'});       }       user.comparepassword(password, user.password, function(err, ismatch){         if(err) throw err;         if(ismatch){ return done(null, user)}         else{           console.log('invalid password');           return done(null, false,  {message:'invalid password'});         }       });     });   } ));   router.post('/login', passport.authenticate('local',{failureredirect:'/users/login',failureflash: true }), function(req, res){   console.log('auth sucessful');   req.flash('sucess','logged in');   res.redirect('/'); });   module.exports = router; 

and usermodel.js has comparepassword , getuserbyusername.

var mongoose = require('mongoose'); var bcrypt = require('bcrypt'); mongoose.connect('mongodb://localhost/nodeauth'); var db = mongoose.connection;  //schema var userschema = mongoose.schema({   username:{     type:string,     index:true   },   password:{     type:string,     bcrypt :true,     required: true   },   email:string,    name:string,    profileimage:{     type:string   } });    var user = module.exports = mongoose.model('user',userschema);  module.exports.comparepassword = function(candidatepassword, hash , callback){   bcrypt.compare(candidatepassword, hash, function(err, ismatch){     if(err) return callback(err);     callback(null, ismatch);   }); }  module.exports.getuserbyusername = function(username, callback){   var query = {username : username};   user.findone(query, function(err, user) {     callback(err, user);   });  }  module.exports.getuserbyid = function(id, callback){   user.findbyid(id,callback); }  module.exports.createuser = function(newuser,callback){   bcrypt.hash(newuser.password, 10, function(err, hash){     if(err) throw err;     //set hash password     newuser.password = hash;     newuser.save(callback);   } ); } 

  function(username, password, done){      user.getuserbyusername(username, function(err, user){       if(err) throw err;       if(!user){         console.log('unknown user');         return done(null, false,{message:'unkown user'});       }        user.comparepassword(password, user.password, function(err, ismatch){         if(err) throw err;         if(ismatch){ return done(null, user)}         else{           console.log('invalid password');           return done(null, false,  {message:'invalid password'});         }        });     });    } 

this problem should occur. since if(err) throw err; present , you're not getting errors, whatever happens not considered error, must logic fails @ point.

you should test, whether passwords return properly, or if bcrypt comparation in usermodel works should. these main points can see critical now.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -