Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passport js missing credentials

Been working on this for a few hours now, pretty frustrating...

router.post('/', passport.authenticate('local-signup', function(err, user, info) {
  console.log(err);
}), function(req, res){
  console.log(req);
  res.setHeader('Content-Type', 'application/json');
  res.send(JSON.stringify({ a: 1 }));
});

When I run this, i used console.log, output was { message: 'Missing credentials' }, which leads me to believe that the body parser is not properly parsing the body message. When I use this route however...

router.post('/', function(req, res){
  console.log(req.body);
  res.setHeader('Content-Type', 'application/json');
  res.send(JSON.stringify({ a: 1 }));
});

When I used console.log, the output was {password: 'password', email: '[email protected]'}, which indicates that the req.body variables are being set properly and are available.

app.js

var express = require('express');
var app = express();
var routes = require("./config/routes.config");
var models = require("./config/models.config");
var session = require('express-session');
var bodyParser = require('body-parser');

models.forEach(function(model){
  GLOBAL[model] = require('./models/'+model+".model");
});
var passport = require("./config/passport.config");

app.use( bodyParser.urlencoded({ extended: true }) );
app.use(session({ secret: 'simpleExpressMVC', resave: true, saveUninitialized: true  }));
app.use(passport.initialize());
app.use(passport.session());
like image 975
l2silver Avatar asked Sep 06 '25 15:09

l2silver


2 Answers

I see your req.body contains {password: 'password', email: '[email protected]'}. email is not what passportjs is looking for, but username is. You can either change your input names on your HTML/JS, or you can change the default parameters passportjs is looking for in req.body. You need to apply these changes where you define your strategy.

passport.use(new LocalStrategy({ // or whatever you want to use
    usernameField: 'email',    // define the parameter in req.body that passport can use as username and password
    passwordField: 'password'
  },
  function(username, password, done) { // depending on your strategy, you might not need this function ...
    // ...
  }
));
like image 174
Aᴍɪʀ Avatar answered Sep 11 '25 01:09

Aᴍɪʀ


In Router.post:

router.post('/', passport.authenticate('local-signup', {
    successRedirect: '/dashboad',
    failureRedirect: '/',
    badRequestMessage: 'Your message you want to change.', //missing credentials
    failureFlash: true
}, function(req, res, next) {
...
like image 44
IT Vlogs Avatar answered Sep 11 '25 01:09

IT Vlogs