我在我的服务器上使用 Passportjs 进行身份验证,我想知道是否在客户端设置了任何值来告诉客户端他们是否已登录。如果没有,您将如何向客户端提供此信息? 客户端是否有权访问有关会话的信息?
谢谢!
我一直在努力通过 Node.js 为数据库构建一个简单的 REST API,并希望为不同用户实现具有授权规则的基本单点登录结构。我开始使用护照(特别是本地护照),它在身份验证和轻松登录/注销服务方面做得很好——但似乎对无状态设置不友好。
在进一步研究中,我遇到了cansecurity并认为它具有我正在寻找的功能。阅读完文档后,我在真正进入这个过程时遇到了一些问题:
1)我如何实际发送登录/注销请求?
我看到了 cansecurity 如何允许对不同的路由进行授权,并且我编写了一个验证函数来检查散列密码、检索用户等。但是有没有人对实际执行单点登录以使用户登录有任何了解?
2) 是否有一种简单的方法可以将访问信息集成到外部源中?
理想情况下,我希望有一个使用 API 来执行数据库操作的移动应用程序(因此希望拥有 SSO 和相当强大的授权框架)。
如果有人使用过 Passport 或 cansecurity 进行过类似的设置,你是怎么做的,我可能会遗漏哪些步骤?
设置安全:
var cs = require('cansecurity')
, cansec = cs.init(
{
sessionExpiry: 60,
sessionKey: SESSIONKEY,
validate:
function(username, password, done) {
User.findOne({ username: username }, function(err, user) {
if (err) return done(err);
if (!user) {
return done(false, null, { message: 'Incorrect username.' }, null);
}
if(password !== undefined){
user.validPassword(password, function(err, result) {
if (result == true) …Run Code Online (Sandbox Code Playgroud) 我有一个简单的node.js代码,它使用mongoose,它在保存但无法检索时有效.
.save()有效,但.findOne()没有.
mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/TestMongoose");
UserSchema = new mongoose.Schema({
field: String
});
Users = mongoose.model('userauths', UserSchema);
user = new Users({
field: 'value'
});
//user.save();
Run Code Online (Sandbox Code Playgroud)
^工作.即用值更新数据库.截图
//user.findOne({field:'value'},function(err,value){});
Run Code Online (Sandbox Code Playgroud)
^引发错误:
user.findOne({field:'value'},function(err,value){});
^
TypeError: Object { field: 'value', _id: 52cd521ea34280f812000001 } has no method 'findOne'
at Object.<anonymous> (C:\localhost\nodeTest\z.js:16:6)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
Run Code Online (Sandbox Code Playgroud)
console.log(JSON.stringify( user , null, 40));
Run Code Online (Sandbox Code Playgroud)
^只返回对象 {field: 'value'}
console.log(JSON.stringify( Users , …Run Code Online (Sandbox Code Playgroud) console.log('>>>>>>user = '+ user);
Run Code Online (Sandbox Code Playgroud)
产出
>>>>>>user = { username: 'user1',
salt: '3303187e50a64889b41a7a1c66d3d3c10b9dec638fdd033bee1221d30d01c5e1',
hash: 'a174c206d88bee1594bb081dbd32d53420f6ef3d6322104f3d0722d58bc8dd8d',
_id: 52d3177481daf59c11000001,
__v: 0 }
Run Code Online (Sandbox Code Playgroud)
但
console.log('>>>>>>user.hash = '+ user.hash);
Run Code Online (Sandbox Code Playgroud)
产出
>>>>>>user.hash = undefined
Run Code Online (Sandbox Code Playgroud)

什么可能导致这种情况?
编辑:有趣的是,user._id, (并且只有它)有效。

如何防止我的用户同时从两个设备登录我的系统?因此,如果用户从计算机登录,当他从不同的计算机登录时,会话首先自动关闭(不需要实时).
我使用node.js,express.js,mongoose,passport,connect-mongo(在数据库中存储会话).
我使用的是护照,并希望使用Google Contacts API 3.0版将Google通讯录与我的应用程序同步(这会突然变得有用10倍).
有人这样做过吗?如果是这样,你有一些示例代码吗?是否可以使用护照身份验证使其全部正常工作?
我正在尝试在我的node.js应用程序中使用passport.js来验证使用MongoDB的用户.我从来没有成功过.
在帖子中,我发送"email":"email@gmail.com","密码":"测试"
var passport = require('passport')
, LocalStrategy = require('passport-local').Strategy;
app.post('/login',
passport.authenticate('local', { successRedirect: '/',
failureRedirect: '/fail' })
);
passport.use(new LocalStrategy({
emailField: 'email',
passwordField: 'passw',
},
function (emailField, passwordField, done) {
process.nextTick(function () {
db.collection(users, function (error, collection) {
if (!error) {
collection.findOne({
'email': emailField,
'password': passwordField
}, function (err, user) {
if (err) {
return done(err);
}
if (!user) {
console.log('this email does not exist');
return done(null, false);
}
return done(null, user);
});
} else {
console.log(5, …Run Code Online (Sandbox Code Playgroud) 我使用passport-twitter在我的网站上建立了一个twitter连接.用户可以通过单击"登录"或"添加新项目"进行连接.2之间的唯一区别是,如果他们点击添加新项目,则一旦他们登录就会打开模态窗口.
要知道他们点击的按钮,我将网址存储在req.session.referrer:
// route for twitter authentication and login
app.get('/auth/twitter', function(req, res, next){
req.session.referrer = req.url;
console.log(req.session);
passport.authenticate('twitter')(req, res, next);
});
app.get('/auth/twitter/new', function(req, res, next){
req.session.referrer = req.url;
console.log(req.session);
passport.authenticate('twitter')(req, res, next);
});
// handle the callback after twitter has authenticated the user
app.get('/auth/twitter/callback', function(req, res, next){
var options = {
successRedirect : '/twitter-user/signin',
failureRedirect : '/'
};
console.log(req.session);
if (req.session.referrer && req.session.referrer.indexOf('new') > -1) options.successRedirect = '/twitter-user/new';
passport.authenticate('twitter', options)(req, res, next)
});
Run Code Online (Sandbox Code Playgroud)
在我的开发环境中一切正常,但一旦在线,我收到此错误消息:Express
500 Error: Failed to …Run Code Online (Sandbox Code Playgroud) 我面临一个非常奇怪的情况,与mongoDb的初始连接大约需要15秒。我当前的设置如下:
重新启动nodemon后,初始登录POST大约需要15秒
POST /api/v1/signin 200 14707ms - 56b
Run Code Online (Sandbox Code Playgroud)
在不重新启动服务器的情况下将其他POST发送到同一路由相对较快:
POST /api/v1/signin 200 76ms - 56b
Run Code Online (Sandbox Code Playgroud)
之所以困扰我,是因为该项目仍在开发中,nodemon往往会大量重启,而测试却很痛苦。
我正在使用以下与数据库和身份验证有关的节点模块:
这是我连接到mongo的方式:
var mongoUrl = "mongodb://devmachine.local:27017/project";
mongoose.connect(mongoUrl, {auto_reconnect: true});
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激。
谢谢
node.js ×8
passport.js ×7
mongoose ×3
express ×2
javascript ×2
mongodb ×2
google-api ×1
oauth-2.0 ×1
powershell ×1
process ×1
rest ×1
sync ×1
twitter ×1