使用 mongodb 和 nodejs 在 passportjs 中授权多个本地策略
authorization for multiple local strategies in passportjs using mongodb and nodejs
我正在使用 passportjs 和 mongodb 在 nodejs 中实施多个本地身份验证策略。为了实现身份验证,我在 deserializeUser 中使用了中间件。像这样的东西。我有两种类型的用户,一种是User,另一种是Vendor。供应商将销售产品,用户将购买产品。
我为具有不同本地策略名称的供应商和用户创建了不同的模式。身份验证对我来说很好。
module.exports = function(passport){
// Passport needs to be able to serialize and deserialize users to support persistent login sessions
passport.serializeUser(function(user, done) {
console.log('user id is: ' + user._id);
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function (err, user) {
if(err)
done(err);
if(user) {
done(null, user);
}
else {
Vendor.findById(id, function (err, user) {
if(err)
done(err);
done(null, user);
});
}
});
});
我想做的是,'User' 帐户不能访问供供应商使用的页面,供应商也不能访问供用户使用的页面。通过这种方式,我想提供对这两种类型的用户帐户共享的数据的访问控制。
我认为护照是不可能的,但我们需要为它写一些中间件。我们可以通过一些中间件来实现它吗,或者 npm 中是否有任何我们可以用于此目的的包。如果有人能举出一些简单的中间件的例子,那对我来说真的很有帮助。
我解决了这个问题,方法是在 'User' 模式中引入一个字段 'isUser' 并在 'Vendor' 模式中引入 'isVendor' 并将其类型保留为布尔值。现在,当用户注册为 'User' 时,它将 'isUser' 存储为 true,当 'Vendor' 注册时,它将 'isVendor' 存储为 true。我们将 vendor 和 User 存储在两个不同的模式中。
现在在route handler中,我写了一个中间件,大概是这样的。
var isVendor = function (req, res, next) {
if (req.user.isVendor === true) {
return next();
}
res.redirect('/vendorlogin');
};
var isUser = function (req, res, next) {
if(req.user.isUser === true) {
return next();
}
res.redirect('/login');
};
现在我将它添加到我的路由处理程序中,就像这样。
/**
* Testing the authorization policy using two different local strategy
*/
router.get('/productsvendor', isAuthenticated, isVendor, function (req, res) {
res.send('this is product vendor page, which should be seen only by vendors');
});
router.get('/itemuser', isAuthenticated, isUser , function (req, res) {
res.send('this is item user page, which should be seen only by users');
});
我正在使用 passportjs 和 mongodb 在 nodejs 中实施多个本地身份验证策略。为了实现身份验证,我在 deserializeUser 中使用了中间件。像这样的东西。我有两种类型的用户,一种是User,另一种是Vendor。供应商将销售产品,用户将购买产品。 我为具有不同本地策略名称的供应商和用户创建了不同的模式。身份验证对我来说很好。
module.exports = function(passport){
// Passport needs to be able to serialize and deserialize users to support persistent login sessions
passport.serializeUser(function(user, done) {
console.log('user id is: ' + user._id);
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function (err, user) {
if(err)
done(err);
if(user) {
done(null, user);
}
else {
Vendor.findById(id, function (err, user) {
if(err)
done(err);
done(null, user);
});
}
});
});
我想做的是,'User' 帐户不能访问供供应商使用的页面,供应商也不能访问供用户使用的页面。通过这种方式,我想提供对这两种类型的用户帐户共享的数据的访问控制。 我认为护照是不可能的,但我们需要为它写一些中间件。我们可以通过一些中间件来实现它吗,或者 npm 中是否有任何我们可以用于此目的的包。如果有人能举出一些简单的中间件的例子,那对我来说真的很有帮助。
我解决了这个问题,方法是在 'User' 模式中引入一个字段 'isUser' 并在 'Vendor' 模式中引入 'isVendor' 并将其类型保留为布尔值。现在,当用户注册为 'User' 时,它将 'isUser' 存储为 true,当 'Vendor' 注册时,它将 'isVendor' 存储为 true。我们将 vendor 和 User 存储在两个不同的模式中。 现在在route handler中,我写了一个中间件,大概是这样的。
var isVendor = function (req, res, next) {
if (req.user.isVendor === true) {
return next();
}
res.redirect('/vendorlogin');
};
var isUser = function (req, res, next) {
if(req.user.isUser === true) {
return next();
}
res.redirect('/login');
};
现在我将它添加到我的路由处理程序中,就像这样。
/**
* Testing the authorization policy using two different local strategy
*/
router.get('/productsvendor', isAuthenticated, isVendor, function (req, res) {
res.send('this is product vendor page, which should be seen only by vendors');
});
router.get('/itemuser', isAuthenticated, isUser , function (req, res) {
res.send('this is item user page, which should be seen only by users');
});