- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我真的很难让 passport
在我的 Node.js 应用程序中工作。我尝试在我的 app.js
中重新排序我的要求,但我仍然无法让它工作。这是我得到的错误:
Not Found
404
Error: Not Found
at /home/salma/Desktop/my-project/app.js:56:13
at Layer.handle [as handle_request] (/home/salma/Desktop/my-project/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/salma/Desktop/my-project/node_modules/express/lib/router/index.js:312:13)
at /home/salma/Desktop/my-project/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/home/salma/Desktop/my-project/node_modules/express/lib/router/index.js:330:12)
at next (/home/salma/Desktop/my-project/node_modules/express/lib/router/index.js:271:10)
at /home/salma/Desktop/my-project/node_modules/express/lib/router/index.js:618:15
at next (/home/salma/Desktop/my-project/node_modules/express/lib/router/index.js:256:14)
at next (/home/salma/Desktop/my-project/node_modules/express/lib/router/route.js:121:14)
at complete (/home/salma/Desktop/my-project/node_modules/passport/lib/middleware/authenticate.js:250:13)
at /home/salma/Desktop/my-project/node_modules/passport/lib/middleware/authenticate.js:257:15
at pass (/home/salma/Desktop/my-project/node_modules/passport/lib/authenticator.js:421:14)
at Authenticator.transformAuthInfo (/home/salma/Desktop/my-project/node_modules/passport/lib/authenticator.js:443:5)
at /home/salma/Desktop/my-project/node_modules/passport/lib/middleware/authenticate.js:254:22
at /home/salma/Desktop/my-project/node_modules/passport/lib/http/request.js:60:7
at pass (/home/salma/Desktop/my-project/node_modules/passport/lib/authenticator.js:267:43)
at serialized (/home/salma/Desktop/my-project/node_modules/passport/lib/authenticator.js:276:7)
at /home/salma/Desktop/my-project/config/passport.js:9:9
at pass (/home/salma/Desktop/my-project/node_modules/passport/lib/authenticator.js:284:9)
at Authenticator.serializeUser (/home/salma/Desktop/my-project/node_modules/passport/lib/authenticator.js:289:5)
at IncomingMessage.req.login.req.logIn (/home/salma/Desktop/my-project/node_modules/passport/lib/http/request.js:50:29)
at Strategy.strategy.success (/home/salma/Desktop/my-project/node_modules/passport/lib/middleware/authenticate.js:235:13)
这是我的代码:
app.js:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var flash = require('connect-flash');
var passport = require('passport');
var session = require('express-session');
var routes = require('./routes/index');
var userRoutes = require('./routes/user');
var adminRoutes = require('./routes/admin');
var config = require('./config/index.js');
var MongoStore = require('connect-mongo')(session);
var app = express();
mongoose.Promise = global.Promise;
var db = mongoose.connect(config.dbUrl, config.dbOpts);
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
var store = new MongoStore({
mongooseConnection: db.connection
});
app.use(session({
secret: config.sessionSecret,
store: store,
resave: true,
saveUninitialized: true
}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
require('./config/passport')(passport);
app.use('/', routes);
app.use('/', userRoutes);
app.use('/admin', adminRoutes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
mongoose.connection.on('open', function(){
mongoose.connection.db.listCollections(function(error, names) {
if (error) {
throw new Error(error);
} else {
names.map(function(name) {
console.log('found collection %s', name);
});
}
});
});
mongoose.connection.on('error', function(error){
throw new Error(error);
});
module.exports = app;
这是我的登录路径:
routes/user.js:
var express = require('express');
var User = require('../controllers/user');
var passport = require('passport');
var router = express.Router();
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
router.post('/login', passport.authenticate('local',
{ successFlash: 'Success!',
failureFlash : 'Login failed' }));
这是我处理我的passport
身份验证策略的文件:
config/passport.js:
var LocalStrategy = require('passport-local').Strategy;
var User = require('../models/user').model;
var configAuth = require('./auth');
module.exports = function (passport) {
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
passport.use('local', new LocalStrategy({
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true
},
function(req, email, password, done) {
User.findOne({ email : email }, function(err, user) {
if (err) return done(err);
if (!user)
return done(null, false, req.flash('loginMessage', 'This email does not exist.'));
if (!user.validPassword)
return done(null, false, req.flash('loginMessage', 'Oops! The password entered is incorrect.'));
return done(null, user);
});
}));
passport.use(new FacebookStrategy({
clientID: configAuth.facebookAuth.clientID,
clientSecret: configAuth.facebookAuth.clientSecret,
callbackURL: configAuth.facebookAuth.callbackURL,
profileFields: ["emails", "displayName", "name"]
}
}
我已经在同一个路由文件中尝试了其他路由,以确保 app.js
可以看到这些路由并且它们工作正常。只有这个没有。有人可以告诉我做错了什么吗?
最佳答案
问题出在路由选项 successFlash
和 failureFlash
中。 passport.authenticate()
函数需要不同的选项,这些选项必须完全按照这种方式使用,否则它们将不起作用。因此,当我替换选项以完全匹配文档时:
app.post('/login',
passport.authenticate('local', { successRedirect: '/',
failureRedirect: '/login',
failureFlash: true })
);
或
passport.authenticate('local', function(err, user, info) {
if (err) return res.status(500).send();
if (!user) return res.status(400).json({error:info.message});
req.logIn(user, function(err) {
if (err) return next(err);
return res.status(200).json(info.message);
});
})(req, res, next);
它起作用了。还有其他调用函数的方法,它们在此处的文档中列出: http://passportjs.org/docs/authenticate
关于javascript - 在 Express 4 中设置 Passport 时找不到 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41292348/
例如,我有一个父类Author: class Author { String name static hasMany = [ fiction: Book,
代码如下: dojo.query(subNav.navClass).forEach(function(node, index, arr){ if(dojo.style(node, 'd
我有一个带有 Id 和姓名的学生表和一个带有 Id 和 friend Id 的 Friends 表。我想加入这两个表并找到学生的 friend 。 例如,Ashley 的 friend 是 Saman
我通过互联网浏览,但仍未找到问题的答案。应该很容易: class Parent { String name Child child } 当我有一个 child 对象时,如何获得它的 paren
我正在尝试创建一个以 Firebase 作为我的后端的社交应用。现在我正面临如何(在哪里?)找到 friend 功能的问题。 我有每个用户的邮件地址。 我可以访问用户的电话也预订。 在传统的后端中,我
我主要想澄清以下几点: 1。有人告诉我,在 iOS 5 及以下版本中,如果您使用 Game Center 设置多人游戏,则“查找 Facebook 好友”(如与好友争夺战)的功能不是内置的,因此您需要
关于redis docker镜像ENTRYPOINT脚本 docker-entrypoint.sh : #!/bin/sh set -e # first arg is `-f` or `--some-
我是一名优秀的程序员,十分优秀!