I'm trying to use Passport with Compound Js. I've configured the passport in an initialization file. as below
var passport = require('passport')
, LocalStrategy = require('passport-local').Strategy;
passport.use(new LocalStrategy({usernameField: 'email'},
function(email, password, done) {
User.findOne({ email: email }, function(err, user) {
if (err) { return done(err); }
if (!user) {
return done(null, false, { message: 'Incorrect username.' });
}
if (!user.validPassword(password)) {
return done(null, false, { message: 'Incorrect password.' });
}
return done(null, user);
});
}
));
module.exports.passport = passport;
And in my routes file I have:
var passobj = require('./initializers/initialize_passport')
exports.routes = function (map) {
map.post("api/users", passobj.passport.authenticate('local', {successRedirect: 'user#index', failureRedirect: 'user#failureoccured'}));
};
When I tried to call this from firebug by passing a valid username and password, I get the below error:
Undefined action undefined#undefined
Can anyone please tell me how to use Passport with Compound Js.
And also I came accross compound-passport, but don't know if I can use it for local strategy. Thanks in advance.
Here is how I managed to make my local. Hope you can solve your implementation also this way.
It will create a full new strategy that you will be able to use together
var express = require('express');
var passport = require('passport');
var config = require('../config/environment');
var User = require('../api/user/user.model');
// Passport Configuration
require('./login/passport').setup(User, config);
//HERE WE WILL ADD OUR LOCAL STRATEGY
var router = express.Router();
router.use('/local', require('./local/index'));
local/index.js
var passport = require('passport');
var auth = require('../auth.service.js');
var router = express.Router();
router.post('/', function(req, res, next) {
console.log.req;
passport.authenticate('local', function (err, user, info) {
var error = err || info;
if (error) return res.json(401, "THAT´S BAD");
if (!user) return res.json(404, {message: 'Something went wrong, please try again.'});
var token = auth.signToken(user._id, user.role);
res.json({token: token, user:user});
})(req, res, next)
});
module.exports = router;
local/passport.js
var passport = require('passport');
var Local = require('passport-local').Strategy;
exports.setup = function (User, config) {
passport.use("local", new Local({
usernameField: 'apikey',
passwordField: 'apisecret'
},
function(apikey, apisecret, done) {
//THIS FUNCTION IS THE ONE YOU HAVE TO IMPLEMENT TO YOUR LOCAL WAY
User.findOne({
apikey: apikey,
apisecret: apisecret
}, function(err, user) {
if (!user) {
return done("YOUR API KEY HAS NOT AUTHORIZATION", false, { message: 'This email is not registered.' });
}
return done(null, user);
});
}
));
passport.serializeUser(function(user, done) {
done("USER", user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
};