关于 passport.js 如何运作的问题。具体关于 user.id
Questions about how passport.js works. Specifically about user.id
我的 model
、doc
或 record
中没有 id
字段,或者随便你怎么称呼它。那么 done(null, user.id)
是如何工作的。我有 _id
但没有 id
。看起来护照对象正在添加到我与数据库中用户的 _id
的会话中,但它是如何获得 _id
的?它是自动完成的吗?在文档中它说
In this example, only the user ID is serialized to the session
它是怎么得到id的,它是怎么得到用户的?
我的代码中的类似示例:
passport.serializeUser(function(user, done){
done(null, user.id)
});
passport.deserializeUser(function(id, done){
User.findById(id, function(err, user){
done(err, user);
})
});
获取用户id需要哪些步骤?
我也有
passport.use("login", new LocalStrategy(function(username, password, done){
User.findOne({username : username}, function(err, user){
if(err){return done(err)}
if(!user){
return done(null, false, {message : "no User has that name"})
}
.......
我使用 passport.use()
是否以某种方式将 DB
中的用户连接到护照方法。
编辑假设我想通过名称字段上的护照转移文档(用户),我应该使用 user.name 吗?
我也不太确定序列化是如何工作的
So how come done(null, user.id)
works. I have an _id
but no id
.
您正在使用 Mongoose,它向文档添加了所谓的 virtual getter called id
,即 return 是 _id
属性。所以在你的情况下,user.id
和 user._id
都可以工作(return 也一样)。
Does the fact that I use passport.use()
somehow connect the user in the DB to the passport methods.
是也不是。您传递给 passport.use()
的策略实施身份验证过程。 您 必须实施如何根据您的数据库验证输入的用户信息。在示例代码中,调用了 User.findOne()
,并根据提供的信息检查该调用的结果 ("Is username
a valid username? If so, is password
the password that belongs to this user?").
如果用户名和密码匹配,done
回调将以用户文档作为参数调用。该文档是 Passport 将传递给其他各个部分的内容,例如 passport.serializeUser()
(见下文)。
Also I'm not really sure about how serializing works.
序列化用于在 "session object" 中存储有关用户的信息。该对象应该能够唯一标识用户。由于 user.id
(或 user._id
)对用户而言是独一无二的,因此 属性 很好用。
这就是 passport.serializeUser()
的用武之地:它传递给用户文档,它应该为 Passport(通过调用回调函数)提供有关该用户的唯一标识信息。因此 done(null, user.id)
.
会话对象本身也由 "session key" 唯一标识。此密钥存储在 HTTP cookie 中,并在用户成功登录时发送到浏览器。当浏览器打开您网站上的另一个页面时,它会发送该 cookie,并使用 cookie 中的会话密钥,从 "session store".
检索会话对象
会话对象包含唯一用户标识,但不包含其余用户信息。所以这就是 passport.deserializeUser()
的用武之地:它获得了 id,并执行数据库查找以检索属于该 id 的完整用户文档。这就是 Passport 将在您的路由处理程序中作为 req.user
提供的内容。
如果所有这些步骤都成功执行(情况并非总是如此,因为 cookie 可能会过期,用户可能会被删除等),这将意味着 Passport 能够肯定地识别用户,而且他们不必再次登录。至少在会话到期之前不会。
我的 model
、doc
或 record
中没有 id
字段,或者随便你怎么称呼它。那么 done(null, user.id)
是如何工作的。我有 _id
但没有 id
。看起来护照对象正在添加到我与数据库中用户的 _id
的会话中,但它是如何获得 _id
的?它是自动完成的吗?在文档中它说
In this example, only the user ID is serialized to the session
它是怎么得到id的,它是怎么得到用户的?
我的代码中的类似示例:
passport.serializeUser(function(user, done){
done(null, user.id)
});
passport.deserializeUser(function(id, done){
User.findById(id, function(err, user){
done(err, user);
})
});
获取用户id需要哪些步骤?
我也有
passport.use("login", new LocalStrategy(function(username, password, done){
User.findOne({username : username}, function(err, user){
if(err){return done(err)}
if(!user){
return done(null, false, {message : "no User has that name"})
}
.......
我使用 passport.use()
是否以某种方式将 DB
中的用户连接到护照方法。
编辑假设我想通过名称字段上的护照转移文档(用户),我应该使用 user.name 吗?
我也不太确定序列化是如何工作的
So how come
done(null, user.id)
works. I have an_id
but noid
.
您正在使用 Mongoose,它向文档添加了所谓的 virtual getter called id
,即 return 是 _id
属性。所以在你的情况下,user.id
和 user._id
都可以工作(return 也一样)。
Does the fact that I use
passport.use()
somehow connect the user in the DB to the passport methods.
是也不是。您传递给 passport.use()
的策略实施身份验证过程。 您 必须实施如何根据您的数据库验证输入的用户信息。在示例代码中,调用了 User.findOne()
,并根据提供的信息检查该调用的结果 ("Is username
a valid username? If so, is password
the password that belongs to this user?").
如果用户名和密码匹配,done
回调将以用户文档作为参数调用。该文档是 Passport 将传递给其他各个部分的内容,例如 passport.serializeUser()
(见下文)。
Also I'm not really sure about how serializing works.
序列化用于在 "session object" 中存储有关用户的信息。该对象应该能够唯一标识用户。由于 user.id
(或 user._id
)对用户而言是独一无二的,因此 属性 很好用。
这就是 passport.serializeUser()
的用武之地:它传递给用户文档,它应该为 Passport(通过调用回调函数)提供有关该用户的唯一标识信息。因此 done(null, user.id)
.
会话对象本身也由 "session key" 唯一标识。此密钥存储在 HTTP cookie 中,并在用户成功登录时发送到浏览器。当浏览器打开您网站上的另一个页面时,它会发送该 cookie,并使用 cookie 中的会话密钥,从 "session store".
检索会话对象会话对象包含唯一用户标识,但不包含其余用户信息。所以这就是 passport.deserializeUser()
的用武之地:它获得了 id,并执行数据库查找以检索属于该 id 的完整用户文档。这就是 Passport 将在您的路由处理程序中作为 req.user
提供的内容。
如果所有这些步骤都成功执行(情况并非总是如此,因为 cookie 可能会过期,用户可能会被删除等),这将意味着 Passport 能够肯定地识别用户,而且他们不必再次登录。至少在会话到期之前不会。