理解护照序列化反序列化 [英] Understanding passport serialize deserialize

查看:144
本文介绍了理解护照序列化反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解释护照的序列化和反序列化方法的工作流程给外行人。


  1. user.id go after passport.serializeUser 已被调用?


  2. 我们正在调用 passport.deserializeUser ,然后它在工作流程中适合哪里?

      //用于序列化会话的用户
    passport.serializeUser(function(user,done){
    done(null,user.id);
    //这个user.id在哪里?我们应该在任何地方访问吗?
    });

    //用于反序列化用户
    passport.deserializeUser(function(id,done){
    User.findById(id,function(err,user){
    done(err,user);
    });
    });


我仍然试图围绕它。我有一个完整的工作应用程序,没有遇到任何错误。我只是想了解这里发生了什么。任何帮助都不胜感激。

解决方案



  1. user.id在哪里之后的护照.serializeUser已被调用?


用户标识(您提供的第二个参数完成函数)保存在会话中,后来用于通过 deserializeUser 函数检索整个对象。 / p>

serializeUser 确定用户对象的哪些数据应该存储在会话中。 serializeUser方法的结果作为 req.session.passport.user = {} 附加到会话。例如,这将是(因为我们提供用户ID作为密钥) req.session.passport.user = {id:'xyz'}



  1. 我们正在呼叫passport.deserializeUser,在它适合工作流程之后?


deserializeUser 的第一个参数对应于键的用户对象被赋予完成函数(见1.)。所以你的整个对象是通过该键的帮助来检索的。这里的键是用户ID(键可以是用户对象的任何键,即名称,电子邮件等)。
deserializeUser 中,该键与内存数组/数据库或任何数据资源相匹配。



获取的对象以 req.user



Visual Flow 附加到请求对象

  passport.serializeUser(function(user,done){
done(null,user.id);
|
}); |
|
| ____________________>保存到会话req.session.passport.user = {id:'..'}
|
passport.deserializeUser(function(id,done){
________________ |
|
User.findById(id,function(err,user){
done ,用户);
| ______________>用户对象以请求身份附加为req.user

});
});


How would you explain the workflow of Passport's serialize and deserialize methods to a layman.

  1. Where does user.id go after passport.serializeUser has been called?

  2. We are calling passport.deserializeUser right after it where does it fit in the workflow?

    // used to serialize the user for the session
    passport.serializeUser(function(user, done) {
        done(null, user.id); 
       // where is this user.id going? Are we supposed to access this anywhere?
    });
    
    // used to deserialize the user
    passport.deserializeUser(function(id, done) {
        User.findById(id, function(err, user) {
            done(err, user);
        });
    });
    

Im still trying to wrap my head around it. I have a complete working app and am not running into errors of any kind. I just wanted to understand what exactly is happening here. Any help is appreciated.

解决方案

  1. Where does user.id go after passport.serializeUser has been called?

The user id (you provide as the second argument of the done function) is saved in the session and is later used to retrieve the whole object via the deserializeUser function.

serializeUser determines, which data of the user object should be stored in the session. The result of the serializeUser method is attached to the session as req.session.passport.user = {}. Here for instance, it would be (as we provide the user id as the key) req.session.passport.user = {id:'xyz'}

  1. We are calling passport.deserializeUser right after it where does it fit in the workflow?

The first argument of deserializeUser corresponds to the key of the user object that was given to the done function (see 1.). So your whole object is retrieved with help of that key. That key here is the user id (key can be any key of the user object i.e. name,email etc). In deserializeUser that key is matched with the in memory array / database or any data resource.

The fetched object is attached to the request object as req.user

Visual Flow

passport.serializeUser(function(user, done) {
    done(null, user.id);
                 |
});              | 
                 |
                 |____________________> saved to session req.session.passport.user = {id:'..'}
                                   |          
passport.deserializeUser(function(id, done) {
                  ________________|
                  | 
    User.findById(id, function(err, user) {
        done(err, user);
                   |______________>user object attaches to the request as req.user

 });
  });

这篇关于理解护照序列化反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆