node.js - passport login authentication without password field -


i building user registration using nodejs , express. creating new user without passport or authentication strategy. want user should treated logged in once registered successfully. building login api user. user can authentic using email field (as not storing password user). not able validate user getting 'missing credential' in passport authentication.

app.js

var config = require('./config/development'); var passport = require('passport'); var cookieparser = require('cookie-parser'); var bodyparser   = require('body-parser'); var session      = require('express-session');  var  app = express();  //connect db  mongoose.connect(config.db); mongoose.set('debug', config.mongoose.debug);  require('./config/passport')(passport); // pass passport configuration  //initialize models var modelspath = require('path').join(__dirname, 'models'); require('fs').readdirsync(modelspath).foreach(function(file) {   require('./models/' + file); });   // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade');   app.use(logger('dev')); app.use(bodyparser.json()); app.use(expressvalidator());  app.use(bodyparser.urlencoded({   extended: false })); app.use(cookieparser());  /// required passport app.use(session({ secret: 'anystringoftext', saveuninitialized: true, resave: true })); // session secret app.use(passport.initialize()); app.use(passport.session()); // persistent login sessions  app.use(express.static(path.join(__dirname, 'public')));   require('./routes')(app, passport); 

passport.js

var mongoose = require('mongoose'),   localstrategy = require('passport-local').strategy,   user = require('../models/users');  module.exports = function(passport) {   // serialize user id push session   passport.serializeuser(function(user, done) {     done(null, user.id);   });    // deserialize user object based on pre-serialized token   // user id   passport.deserializeuser(function(id, done) {     user.findone({       _id: id     }, function(err, user) {       done(err, user);     });   });    // use local strategy   passport.use(new localstrategy({       usernamefield: 'email',       passreqtocallback : true     },     function(email, password, done) {     user.findone({       email: email     }, function(err, user) {       if (err) {         return done(err);       } else {       return done(null, user);       }     });     }   ));    return passport; }; 

routes/user.js

module.exports = function(app, passport) { app.post('/login', function(req, res, next) {   passport.authenticate('local', function(err, user, info) {     if (user === false) {       res.json('no user found');     } else {       res.json('login successful');     }   })(req, res, next); }); }; 

will please me figure out doing wrong here.

you're halfway there. can use passport-local , have require username field.

you can override username , password field use same key.

passport.use(new localstrategy({   usernamefield: 'email',   passwordfield: 'email',   passreqtocallback : true }, 

then requires include 1 field in login request.


Comments