Using CompoundJs with a passport

I am trying to use Passport with Compound Js. I configured the passport in the 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:

 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, passing a valid username and password, I get the following error:

 Undefined action undefined#undefined 

Can someone tell me how to use Passport with Compound Js.

I also came to a composite passport, but I don’t know if I can use it for a local strategy. Thank you in advance.

+4
source share
1 answer

Here's how I managed to make my local one. Hope you can also solve your implementation.

He will create a complete new strategy that you can 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); }); }; 
0
source

All Articles