我遇到了一个问题,我试图从我单独的配置文件中访问我的护照功能,但是当我在路径文件中需要该文件时,它似乎没有出现护照。错误发生在.post(passportConfig.authenticate('local-login', {
错误:
没有方法'验证' 在对象。 (/Users/user/Desktop/Projects/node/user-app/app/controllers/site-routes.js:68:23)`
passport.js(Passport身份验证逻辑):
module.exports = function(passport){
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var models = require('../models/db-index');
/*==== Passport Configuration ====*/
// Serialize sessions
passport.serializeUser(function(user, done) {
console.log(user.user_id + "Serializing");
done(null, user.user_id);
});
passport.deserializeUser(function(user_id, done) {
models.User.findById(user_id).then(function(user){
done(null, user);
}).error(function(err){
done(err, null);
});
});
//Login Logic
passport.use('local-login', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
},
function(req, email, password, done){
models.User.findOne({ where: { email : email }}).then(function(user) {
if (!user) {
done(null, false, req.flash('loginMessage', 'Unknown User'));
} else if (!user.validPassword(password)) {
done(null, false, req.flash('loginMessage', 'Wrong password'));
} else {
done(null, user);
}
}).catch(function(e) {
done(null, false, req.flash('loginMessage', e.name + " " + e.message));
});
}));
//Sign up Logic
passport.use('local-signup', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
}, function(req, email, password, done){
models.User.findOne({ where: { email: email }}).then(function(existingUser){
if (existingUser)
return done(null, false, req.flash('loginMessage', 'That email is already taken.'));
if (req.user) {
var user = req.user;
user.email = email;
user.password = models.User.generateHash(password);
user.save().catch(function (err){
throw err;
}).then(function(){
done(null, user);
});
} else {
var newUser = User.build({
email: email,
password: models.User.generateHash(password)
});
newUser.save().then(function(){ done(null, newUser);
}).catch(function(err){
done(null, false, req.flash('loginMessage', err));
});
}
}).catch(function(e){
done(null, false, req.flash('loginMessage',e.name + " " + e.message));
})
}));
};
站点routes.js:
var express = require('express');
var siteRoutes = express.Router();
var path = require('path');
var passportConfig = require(path.resolve(__dirname, '..', '..','./config/passport.js'));
var models = require('../models/db-index');
/*==== Index ====*/
siteRoutes.get('/', function(req, res){
res.render('pages/index.hbs');
});
siteRoutes.get('/flash', function(req, res){
req.flash('test', 'it worked');
res.redirect('/')
});
/*==== Login ====*/
siteRoutes.route('/login')
.get(function(req, res){
res.render('pages/login.hbs');
})
.post(passportConfig.authenticate('local-login', {
successRedirect: '/app',
failureRedirect: '/login',
failureFlash: 'Invalid username or password.'
}));
siteRoutes.route('/sign-up')
.get(function(req, res){
res.render('pages/sign-up.hbs');
})
.post(function(req, res){
models.User.create({
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
password: req.body.password
}).then(function() {
res.redirect('/app');
//res.redirect('/sign-up/create-organization');
}).catch(function(error){
res.send(error);
})
});
module.exports = siteRoutes;
答案 0 :(得分:1)
当您require
config/passport.js
时,passportConfig
将被初始化为函数,因为您在module.exports
文件中为passport.js
分配了一个函数。我指的是这一行:
module.exports = function(passport){
我要做的就是摆脱这个功能。让代码执行,然后将passport
分配给module.exports
。 .authenticate
方法位于passport
对象上。
像这样:
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var models = require('../models/db-index');
/*==== Passport Configuration ====*/
// Serialize sessions
passport.serializeUser(function(user, done) {
console.log(user.user_id + "Serializing");
done(null, user.user_id);
});
passport.deserializeUser(function(user_id, done) {
models.User.findById(user_id).then(function(user){
done(null, user);
}).error(function(err){
done(err, null);
});
});
//Login Logic
passport.use('local-login', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
},
function(req, email, password, done){
models.User.findOne({ where: { email : email }}).then(function(user) {
if (!user) {
done(null, false, req.flash('loginMessage', 'Unknown User'));
} else if (!user.validPassword(password)) {
done(null, false, req.flash('loginMessage', 'Wrong password'));
} else {
done(null, user);
}
}).catch(function(e) {
done(null, false, req.flash('loginMessage', e.name + " " + e.message));
});
}));
//Sign up Logic
passport.use('local-signup', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
}, function(req, email, password, done){
models.User.findOne({ where: { email: email }}).then(function(existingUser){
if (existingUser)
return done(null, false, req.flash('loginMessage', 'That email is already taken.'));
if (req.user) {
var user = req.user;
user.email = email;
user.password = models.User.generateHash(password);
user.save().catch(function (err){
throw err;
}).then(function(){
done(null, user);
});
} else {
var newUser = User.build({
email: email,
password: models.User.generateHash(password)
});
newUser.save().then(function(){ done(null, newUser);
}).catch(function(err){
done(null, false, req.flash('loginMessage', err));
});
}
}).catch(function(e){
done(null, false, req.flash('loginMessage',e.name + " " + e.message));
})
}));
module.exports = passport;