Skip to content Skip to sidebar Skip to footer

Passportjs, Local Strategy Cannot Be Found

I am new to javascript and node. I followed the guide from passportJS and I am getting the error 'local strategy cannot be found'. I do not know why. my code, basically taken from

Solution 1:

Here's a boilerplate for using passport-local. The order in which the middleware is configured matters. It also implements serializeUser/deserializeUser which seem to be missing from your code.

var express = require('express')
, http = require('http')
, path = require('path')
, passport = require('passport')
, LocalStrategy = require('passport-local').Strategy;

var app = express();

passport.use(new LocalStrategy(function(username, password, done) { 
  // insert your MongoDB check here. For now, just a simple hardcoded check.if (username === 'foo' && password === 'bar')
  {
    done(null, { user: username });
  }
  else
  {
    done(null, false);
  }
}));

passport.serializeUser(function(user, done) { 
  // please read the Passport documentation on how to implement this. We're now// just serializing the entire 'user' object. It would be more sane to serialize// just the unique user-id, so you can retrieve the user object from the database// in .deserializeUser().
  done(null, user);
});

passport.deserializeUser(function(user, done) { 
  // Again, read the documentation.
  done(null, user);
});

app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: 'secret' }));
app.use(passport.initialize());
app.use(passport.session());

// route to authenticate the user
app.post('/login', passport.authenticate('local', { 
  successRedirect: '/accessed',
  failureRedirect: '/access'
}));

// app.listen(3012);

When you use curl -v -d "username=foo&password=bar" http://127.0.0.1:3012/login you see you'll get redirected to /accessed, meaning the authentication worked.

Post a Comment for "Passportjs, Local Strategy Cannot Be Found"