gpt4 book ai didi

mysql - 使用 Node 模块化 Passport.js 时出现问题

转载 作者:行者123 更新时间:2023-11-29 07:52:27 26 4
gpt4 key购买 nike

所以我在这里遇到了一个问题。我有我的登录路线:

var express = require('express');
var router = express.Router();
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
require('../config/passport')(passport, LocalStrategy);


/* GET /login */
router.get('/', function(req, res) {
//you'll probably write some
res.render('login', { title: 'Please Log In' });
});

router.post('/', function(req, res) {
console.log('posting');
console.log(passport);
passport.initialize();
passport.session();
passport.authenticate('local', { successRedirect: '/',
failureRedirect: '/login'
});
console.log('after');
});
module.exports = router;

我的app.js:

var express = require('express'),
path = require('path'),
favicon = require('serve-favicon'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
session = require('express-session'),
RedisStore = require('connect-redis')(session),
passport = require('passport'),

// LocalStrategy = require('passport-local').Strategy,
pool = require('./config/database'),

routes = require('./routes/index'),
api = require('./routes/api'),
login = require('./routes/login'),

app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session(<<redis store info>>);
app.use(passport.initialize());
app.use(passport.session());


app.use('/', routes);
app.use('/api', api);
app.use('/login', login);
...etc

和我的./config/passport.js

var pool = require('./database');

module.exports = function(passport, LocalStrategy) {


passport.serializeUser(function(user, done) {
done(null, user.id);
});

// used to deserialize the user
passport.deserializeUser(function(id, done) {
mysql.query("select * from users where id = "+id,function(err,rows){
done(err, rows[0]);
});
});

passport.use(new LocalStrategy({
usernameField : 'username',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, username, password, done) { // callback with user and password from our form
console.log('hi');
pool.query("SELECT * FROM `users` WHERE `user` = '" + username + "'",function(err,rows){
if (err)
return done(err);
if (!rows.length) {
return done(null, false, req.flash('loginMessage', 'No user found.')); // req.flash is the way to set flashdata using connect-flash
}

// if the user is found but the password is wrong
if (!( rows[0].password == password))
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata

// all is well, return successful user
return done(null, rows[0]);

});

}));

};

我真的无法理解为什么 Passport 配置似乎根本没有得到处理。当我调用 Passport.authenticate 时,它​​似乎什么也没做

最佳答案

所以,我做了一些事情来让它发挥作用。首先,我将以下内容移至 Passport.js 中,在我的 route 包含与 require() Passport 相关的任何内容都是疯狂的。我一开始并没有遇到这种困惑,但尝试修补已经进展到了这一点。此外,我的反序列化函数错误地使用了 mysql 引用,而不是我的池。

var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;

然后我就像在配置中一样设置了 Passport ,然后就这样做了

module.exports = passport;

然后在 app.js 中执行此操作:

var passport = require('../config/passport');

然后我改变了我的路由器,删除了 post 函数中的所有内容,然后做了:

router.post('/', passport.authenticate('local', { successRedirect: '/',
failureRedirect: '/login'
}));

我了解到我不能只调用passport.authenticate。它应该被放置在请求处理程序中。

很抱歉提出了完全疯狂和模棱两可的问题。以防万一任何可怜的灵魂会寻找这个,我想我至少需要有礼貌地谈论它。

关于mysql - 使用 Node 模块化 Passport.js 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26078381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com