Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
Passport LocalStrategy never called on Express

I wrote console.log in LocalStrategy callback but it never called. I use Sequelize for ORM (MySQL).

passport.js

const LocalStrategy = require('passport-local').Strategy
const passport = require('passport')
const bcrypt = require('bcrypt')
const UrunModel = require('../models/Urun')

console.log('File is Called') // If my require is wrong. But this log worked.    

passport.use(new LocalStrategy({
    usernameField: 'mail',
    passwordField: 'pw1'
},
    (username, password, done) => {
      console.log('Worked!') // Never Called
      UrunModel.findOne({
          where : {
              UrunName : 'testuser',
              UrunDesc : '123456'
          }
      })
    }
));

passport.serializeUser(function(user, done) {
    done(null, user.id);
  });
  
  passport.deserializeUser(function(id, done) {
    User.findById(id, function(err, user) {
      done(err, user);
    });
  });

index.js

const session = require('express-session');
const bodyParser = require('body-parser')
const urlencoded = bodyParser.urlencoded({extended:false})
const passport = require('passport')

app.use(session({
  secret: 'keyboardSecrIncKey',
  resave: false,
  saveUninitialized: true,
}))

app.use(passport.initialize())
app.use(passport.session())

require('./config/passport') //require my passport.js

app.post('/test',(req,res,next) => {
  passport.authenticate('local',{
    successRedirect: '/worked',
    failureRedirect: '/notworked'
  })(req,res,next)
})

It redirect me '/notworked' url. I watched 3 or 4 videos but almost same on my code.

Where i am failed ? Please explain my fault.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

By default, LocalStrategy expects to find credentials in parameters named username and password. If your site prefers to name these fields differently, options are available to change the defaults.

you use mail and pw1 parameters , check req.body, because req.body don't have this parameters in the body.

your issue will be solve if you replace

{
    usernameField: 'mail',
    passwordField: 'pw1'
}

with

{
    usernameField: 'username',
    passwordField: 'password'
}

Also you can check the documentation

over 4 years ago · Santiago Trujillo Report

0

Solved:

LocalStrategy not called when there are empty fields(usernameField & passwordField)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!