快速护照会话不工作
Express Passport Session not working
我正在构建一个 Node 应用程序,用户必须在其中注册或登录,然后当他们拖放一些元素时(前端全部工作)我将他们的操作及其相应的用户 ID 存储在数据库中。
我的理解是,一旦他们进入 registered/logged,我就可以使用 req.user 访问他们的 ID 并正确存储他们的操作,但是它不起作用。
这是我的 server.js 文件中处理 Passport 的部分。另外,我将 Sequelize 用作 ORM,但是在没有 req.user 部分的情况下,处理数据库的所有内容都可以正常工作。
app.use(cookieParser());
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(passport.session());
/****** Passport functions ******/
passport.serializeUser(function (user, done) {
console.log('serialized');
done(null, user.idUser);
});
passport.deserializeUser(function (id, done) {
console.log("start of deserialize");
db.user.findOne( { where : { idUser : id } } ).success(function (user) {
console.log("deserialize");
console.log(user);
done(null, user);
}).error(function (err) {
done(err, null);
});
});
//Facebook
passport.use(new FacebookStrategy({
//Information stored on config/auth.js
clientID: configAuth.facebookAuth.clientID,
clientSecret: configAuth.facebookAuth.clientSecret,
callbackURL: configAuth.facebookAuth.callbackURL,
profileFields: ['id', 'emails', 'displayName', 'name', 'gender']
}, function (accessToken, refreshToken, profile, done) {
//Using next tick to take advantage of async properties
process.nextTick(function () {
db.user.findOne( { where : { idUser : profile.id } }).then(function (user, err) {
if(err) {
return done(err);
}
if(user) {
return done(null, user);
} else {
//Create the user
db.user.create({
idUser : profile.id,
token : accessToken,
nameUser : profile.displayName,
email : profile.emails[0].value,
sex : profile.gender
});
//Find the user (therefore checking if it was indeed created) and return it
db.user.findOne( { where : { idUser : profile.id } }).then(function (user, err) {
if(user) {
return done(null, user);
} else {
return done(err);
}
});
}
});
});
}));
/* FACEBOOK STRATEGY */
// Redirect the user to Facebook for authentication. When complete,
// Facebook will redirect the user back to the application at
// /auth/facebook/callback//
app.get('/auth/facebook', passport.authenticate('facebook', { scope : ['email']}));
/* FACEBOOK STRATEGY */
// Facebook will redirect the user to this URL after approval. Finish the
// authentication process by attempting to obtain an access token. If
// access was granted, the user will be logged in. Otherwise,
// authentication has failed.
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { failureRedirect: '/' }),
function (req, res) {
// Successful authentication, redirect home.
res.redirect('../../app.html');
});
app.get('/', function (req, res) {
res.redirect('/');
});
app.get('/app', isLoggedIn, function (req, res) {
res.redirect('app.html');
});
app.post('/meal', function (req, res) {
//Testing Logs
/*console.log(req.body.foodId);
console.log(req.body.quantity);
console.log(req.body.period);
console.log(req.body);
*/
//Check whether or not this is the first food a user drops on the diet
var dietId = -1;
db.diet.findOne( { where : { userIdUser : req.user.idUser } } ).then(function (diet, err) {
if(err) {
return done(err);
}
if(diet) {
dietId = diet.idDiet;
} else {
db.diet.create( { userIdUser : req.user.idUser }).then(function (diet) {
dietId = diet.idDiet;
});
}
});
db.meal.create({
foodId : req.body.foodId,
quantity : req.body.quantity,
period : req.body.period
}).then(function (meal) {
console.log(meal.mealId);
res.json({ mealId : meal.mealId});
});
});
根据我在 Passport 文档中阅读的内容,每当我使用 req.user 时都应该调用我实现的 deserializeUser 函数,但是,对于我的 console.logs(),我发现 serializeUser登录后调用,因此它存储我的会话,但从未调用 deserializeUser!曾经。
知道如何解决这个问题吗?感谢任何帮助,谢谢!
您需要 express session middleware before calling passport.session()
. Read the passportjs configuration section 文档了解更多信息。
- 确保在设置
passport.session
中间件之前设置cookieParser
和express-session
中间件:
const cookieParser = require('cookie-parser')
const session = require('express-session')
app.use(cookieParser());
app.use(session({ secret: 'secret' }));
app.use(passport.initialize());
app.use(passport.session());
- 要测试 passport 会话是否有效,请使用:
console.log(req.session.passport.user)
(例如放在中间件上)
- 在我的例子中,我使用的是 LocalStrategy,我想我可以使用简单的用户名和密码作为表单参数来保护端点,而且我虽然 passport 只会在找不到会话中的用户时才使用表单参数。但这是错误的假设。在 passport localStrategy 中,
login
和 protected endpoint.
应该有单独的端点
因此请确保您为每个端点使用正确的中间件。就我而言:
错误:
受保护的端点:
app.get('/onlyformembers', passport.authenticate('local'), (req, res) => {
res.send({"res": "private content here!"})
})
正确 :
登录:
app.post('/login', passport.authenticate('local'), (req, res) => {
res.send('ok')
})
受保护的端点:
var auth = function (req, res, next) {
if (req.isAuthenticated())
return next();
res.status(401).json("not authenticated!");
}
app.get('/onlyformembers', auth, (req, res) => {
res.send({"res": "private content here!"})
})
我正在构建一个 Node 应用程序,用户必须在其中注册或登录,然后当他们拖放一些元素时(前端全部工作)我将他们的操作及其相应的用户 ID 存储在数据库中。
我的理解是,一旦他们进入 registered/logged,我就可以使用 req.user 访问他们的 ID 并正确存储他们的操作,但是它不起作用。
这是我的 server.js 文件中处理 Passport 的部分。另外,我将 Sequelize 用作 ORM,但是在没有 req.user 部分的情况下,处理数据库的所有内容都可以正常工作。
app.use(cookieParser());
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(passport.session());
/****** Passport functions ******/
passport.serializeUser(function (user, done) {
console.log('serialized');
done(null, user.idUser);
});
passport.deserializeUser(function (id, done) {
console.log("start of deserialize");
db.user.findOne( { where : { idUser : id } } ).success(function (user) {
console.log("deserialize");
console.log(user);
done(null, user);
}).error(function (err) {
done(err, null);
});
});
//Facebook
passport.use(new FacebookStrategy({
//Information stored on config/auth.js
clientID: configAuth.facebookAuth.clientID,
clientSecret: configAuth.facebookAuth.clientSecret,
callbackURL: configAuth.facebookAuth.callbackURL,
profileFields: ['id', 'emails', 'displayName', 'name', 'gender']
}, function (accessToken, refreshToken, profile, done) {
//Using next tick to take advantage of async properties
process.nextTick(function () {
db.user.findOne( { where : { idUser : profile.id } }).then(function (user, err) {
if(err) {
return done(err);
}
if(user) {
return done(null, user);
} else {
//Create the user
db.user.create({
idUser : profile.id,
token : accessToken,
nameUser : profile.displayName,
email : profile.emails[0].value,
sex : profile.gender
});
//Find the user (therefore checking if it was indeed created) and return it
db.user.findOne( { where : { idUser : profile.id } }).then(function (user, err) {
if(user) {
return done(null, user);
} else {
return done(err);
}
});
}
});
});
}));
/* FACEBOOK STRATEGY */
// Redirect the user to Facebook for authentication. When complete,
// Facebook will redirect the user back to the application at
// /auth/facebook/callback//
app.get('/auth/facebook', passport.authenticate('facebook', { scope : ['email']}));
/* FACEBOOK STRATEGY */
// Facebook will redirect the user to this URL after approval. Finish the
// authentication process by attempting to obtain an access token. If
// access was granted, the user will be logged in. Otherwise,
// authentication has failed.
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { failureRedirect: '/' }),
function (req, res) {
// Successful authentication, redirect home.
res.redirect('../../app.html');
});
app.get('/', function (req, res) {
res.redirect('/');
});
app.get('/app', isLoggedIn, function (req, res) {
res.redirect('app.html');
});
app.post('/meal', function (req, res) {
//Testing Logs
/*console.log(req.body.foodId);
console.log(req.body.quantity);
console.log(req.body.period);
console.log(req.body);
*/
//Check whether or not this is the first food a user drops on the diet
var dietId = -1;
db.diet.findOne( { where : { userIdUser : req.user.idUser } } ).then(function (diet, err) {
if(err) {
return done(err);
}
if(diet) {
dietId = diet.idDiet;
} else {
db.diet.create( { userIdUser : req.user.idUser }).then(function (diet) {
dietId = diet.idDiet;
});
}
});
db.meal.create({
foodId : req.body.foodId,
quantity : req.body.quantity,
period : req.body.period
}).then(function (meal) {
console.log(meal.mealId);
res.json({ mealId : meal.mealId});
});
});
根据我在 Passport 文档中阅读的内容,每当我使用 req.user 时都应该调用我实现的 deserializeUser 函数,但是,对于我的 console.logs(),我发现 serializeUser登录后调用,因此它存储我的会话,但从未调用 deserializeUser!曾经。
知道如何解决这个问题吗?感谢任何帮助,谢谢!
您需要 express session middleware before calling passport.session()
. Read the passportjs configuration section 文档了解更多信息。
- 确保在设置
passport.session
中间件之前设置cookieParser
和express-session
中间件:
const cookieParser = require('cookie-parser')
const session = require('express-session')
app.use(cookieParser());
app.use(session({ secret: 'secret' }));
app.use(passport.initialize());
app.use(passport.session());
- 要测试 passport 会话是否有效,请使用:
console.log(req.session.passport.user)
(例如放在中间件上)
- 在我的例子中,我使用的是 LocalStrategy,我想我可以使用简单的用户名和密码作为表单参数来保护端点,而且我虽然 passport 只会在找不到会话中的用户时才使用表单参数。但这是错误的假设。在 passport localStrategy 中,
login
和protected endpoint.
应该有单独的端点
因此请确保您为每个端点使用正确的中间件。就我而言:
错误:
受保护的端点:
app.get('/onlyformembers', passport.authenticate('local'), (req, res) => {
res.send({"res": "private content here!"})
})
正确 :
登录:
app.post('/login', passport.authenticate('local'), (req, res) => {
res.send('ok')
})
受保护的端点:
var auth = function (req, res, next) {
if (req.isAuthenticated())
return next();
res.status(401).json("not authenticated!");
}
app.get('/onlyformembers', auth, (req, res) => {
res.send({"res": "private content here!"})
})