我正在尝试使用Passport和Compound Js。我已在初始化文件中配置了护照。如下
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;
在我的路线文件中,我有:
var passobj = require('./initializers/initialize_passport')
exports.routes = function (map) {
map.post("api/users", passobj.passport.authenticate('local', {successRedirect: 'user#index', failureRedirect: 'user#failureoccured'}));
};
当我尝试通过传递有效的用户名和密码从firebug调用此函数时,我收到以下错误:
Undefined action undefined#undefined
任何人都可以告诉我如何使用Passport与复合Js。
我也来过复合护照,但不知道我是否可以将它用于本地策略。提前致谢。
答案 0 :(得分:0)
以下是我设法制作本地的方法。希望您也可以通过这种方式解决您的实施问题。
它将创建一个全新的策略,您可以一起使用
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'));
本地/ 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;
本地/ 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);
});
};