javascript - How to use Passport.js and express.js -


i'm trying use first time features of passport.js nothing happens when i'm trying login. can me figure out i've missed or what's wrong code ? here code doesn't work , i'm stuck passport-local feature login username & password.

var express = require('express'); var http = require('http'); var app = express(); var port = 1535; var server = http.createserver(app) var io = require('socket.io').listen(server); var mongoose = require('mongoose'); var passport = require('passport'); var localstrategy = require('passport-local').strategy; var flash = require('connect-flash');  //connection mongodb odm. var db = mongoose.createconnection('localhost', 'test'); db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function callback () { console.log('connected mongodb'); });  //user schema. var userschema = mongoose.schema({ username: string, password: string });  //check user password. userschema.methods.validpassword = function (password) { if (password === this.password) {     return true; } else {     return false; } }  var user = mongoose.model('user', userschema); var user = new user({ username: 'andrew', password: 'secret' }); user.save();  //express.js configuration. app.configure(function(){ app.set('port', process.env.port || 1535); app.use(express.bodyparser()); app.use(express.methodoverride()); app.use(express.cookieparser()); app.use(express.session({ cookie: { maxage: 60000 }, secret: 'keyboard cat' })); app.use(flash()); app.use(passport.initialize()); app.use(passport.session()); app.use(app.router); app.use('/static', express.static(__dirname + '/')); });  //check login form passport.js passport.serializeuser(function(user, done) { done(null, user); });  passport.deserializeuser(function(obj, done) { done(null, obj); });  passport.use(new localstrategy(function(username, password, done) {     user.findone({ username: username }, function(err, user) {         if (err) {             return done(err);         }         if (!user) {             return done(null, false, { message: 'incorrect username.' });         }         if (!user.validpassword(password)) {             console.log('invalid password...')             return done(null, false, { message: 'incorrect password.' });         }         return done(null, user);     }); } ));  app.post('/', passport.authenticate('local', { successredirect: '/home', failureredirect: '/', failureflash: true }) );  //express.js - launch server. server.listen(app.get('port'), function(){ console.log("express server listening on port" + app.get('port')); }); 

your passport.serialize returns user profile. json.stringified , stored in express.session. later passed passport.deserialize returns string rather parsing , returning user profile object.

try:

done(null, json.parse(obj)); 

you may end larger user profile. @ time may want serialize user.id reduce amount of session storage. on deserialization read user profile using id.


Comments