node.js - passportjs validate on signup if user already exists -


i trying create sign user if existing in db logged system, or else new user created in system.

so far have come following code.

//filename passport-config     var config = require('./config'); var passport = require('passport'); var user = require('./models/user'); var localstrategy = require('passport-local').strategy;  var isvalidpassword = function(user, password){     return bcrypt.comparesync(password, user.password); };    // generates hash using bcrypt var createhash = function(password){    return bcrypt.hashsync(password, bcrypt.gensaltsync(10), null); }    // middleware quintessential call next()   // if user authenticated  var isauthenticated = function (req, res, next) {   if (req.isauthenticated())     return next();   res.redirect('/'); }   passport.use('signup', new localstrategy({     passreqtocallback : true   },   function(req, email, password, done) {     findorcreateuser = function(){       // find user in mongo provided email       user.findone({'email':email},function(err, user) {         // in case of error return         if (err){           console.log('error in signup: '+err);           return done(err);         }         // exists         if (user) {              user.findone({ 'email' :  email },                        function(err, user) {                                           if (!user){                             console.log('user not found email '+email);                             return done(null, false);                           }                           // user exists wrong password, log error                            if (!isvalidpassword(user, password)){                              console.log('invalid password');                             return done(null,false);                           }             });          } else {                   // if there no user email                   // create user                   var newuser = new user();                   // set user's local credentials                   newuser.email = email;                   newuser.password = createhash(password);                             // save user                   newuser.save(function(err) {                     if (err){                       console.log('error in saving user: '+err);                         throw err;                       }                     console.log('user registration succesful');                         return done(null, newuser);                   });           }       });     };      // delay execution of findorcreateuser , execute      // method in next tick of event loop      process.nexttick(findorcreateuser);    }) ); 

my router

router.post('/signup', passport.authenticate('signup', {     successredirect: '/timeslot',     failureredirect: '/' }));    server.js file       var express = require('express'); var bodyparser = require('body-parser'); var leisure = require('leisure'); var cors = require('cors'); var passport = require('passport'); var config = require('./config'); var passportconfig = require('./passport-config'); var session = require('express-session') var expresshbs = require('express-handlebars');  var mediatypes = [   { contenttype: 'application/hal+json' },   { contenttype: 'application/json' },   { contenttype: 'text/html' } ];   var app = express();    /*handlebars */ app.engine('handlebars', expresshbs({layout: false}) ); app.set('view engine', 'handlebars');     app.use(express.static(__dirname + '/assets'));  app.use(cors(config.settings.cors)); app.use(bodyparser()); app.use(leisure.accept(mediatypes));   /*sessions */ app.use(session({   secret: 'keyboardsfs23432@@!#!@at' })); app.use(passport.initialize()); app.use(passport.session());       var routes = require('./routes'); app.use('/', routes.router);  function start () {   var port = process.env.port || 3000;   app.listen(port);   console.log('appoints service started on port ' + port); }  exports.app = app;  exports.start = start; 

the signup route doesn't work @ , pretty confused on how debug this, suggestions appreciated.

have @ excellent article at

and sample code at


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -