view raw
cphill cphill - 8 months ago 44
Node.js Question

PassportJS not recognizing record

I'm running into an issue trying to debug why my PassportJS authorization is not working as it should. I tried to add

to each if/else statement, but nothing is appearing in my terminal. The only reason I know that there is an issue is because I am using the correct credentials for a registered user, but I'm getting the failure redirect after submitting the form. Could this be due to the fact that I'm using a different parameter than 'username' and I'm not setting it up correctly? Do I need to pass the values from the form?

Model Index:

var Sequelize = require('sequelize');
var path = require('path');
var config = require(path.resolve(__dirname, '..', '..','./config/config.js'));
var sequelize = new Sequelize(config.database, config.username, config.password, {
dialect: 'mysql'

sequelize.authenticate().then(function(err) {
if (!!err) {
console.log('Unable to connect to the database:', err)
} else {
console.log('Connection has been established successfully.')

var db = {}

db.User = sequelize.import(__dirname + "/user");

db.sequelize = sequelize;
db.Sequelize = Sequelize;


module.exports = db;


module.exports = function(sequelize, DataTypes) {

var User = sequelize.define('users', {
annotation_id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
firstName: {
type: DataTypes.STRING,
field: 'first_name'
lastName: {
type: DataTypes.STRING,
field: 'last_name'
email: DataTypes.STRING,
password: DataTypes.STRING,

}, {
freezeTableName: true,
instanceMethods: {
generateHash: function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
validPassword: function(password) {
return bcrypt.compareSync(password, this.password);
return User;


var express = require('express');
var siteRoutes = express.Router();
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var models = require('../models/db-index');

/*==== Passport Configuration ====*/

// Serialize sessions
passport.serializeUser(function(user, done) {

passport.deserializeUser(function(id, done) {
db.User.find({where: {id: id}}).success(function(user){
done(null, user);
done(err, null);

passport.use(new LocalStrategy({
passReqToCallback : true,
usernameField: 'email',
passwordField : 'password'
}, function(username, password, done) {
//Find user by email
User.findOne({ username: username }, function(err, user) {
//If there is an error, return done
if (err) { return done(err); }
//If user does not exist, log error and redirect
if (!user) {
console.log('No email')
return done(null, false, { message: 'Incorrect username.' });
//If user exists, but wrong password
if (!user.validPassword(password)) {
return done(null, false, { message: 'Incorrect password.' });
console.log('Password fail');
//If all credentials match, return user
return done(null, user);
console.log('Successful login');

/*==== Index ====*/

siteRoutes.get('/', function(req, res){

/*==== Login ====*/


.get(function(req, res){

.post(passport.authenticate('local', {
successRedirect: '/app',
failureRedirect: '/',
failureFlash: true


.get(function(req, res){

.post(function(req, res){

firstName: req.body.firstName,
lastName: req.body.lastName,
password: req.body.password
}).then(function() {


module.exports = siteRoutes;

Login Form:

<!DOCTYPE html>
{{> head}}
{{> navigation}}
<div class="container">
<div class="col-md-6 col-md-offset-3">
<form action="/login" method="post">
<label for="login-username">Username</label>
<input type="text" class="form-control" id="login-username" name="username" placeholder="username or email">
<br />
<label for="login-password">Password</label>
<input type="password" class="form-control" id="login-password" name="password">
<div class="login-buttons">
<button type="submit">Login</button>
<a href="/sign-up">Don't have an account? Then register here!</a>
<br />
<a href="#">Forgot your password?</a>


You specified e-mail as login attribute but used 'username'.

User.findOne({ username: username }

As I see there is no field named username in User model.

I think, you should change username to email.

User.findOne({ email: username }