Serialization and deserialization are an important concept. Serializing an object means converting its state into a byte stream so that the byte stream can be returned back to the copy of the object.
In a typical web application, the credentials used to authenticate the user will only be transmitted during the login request. If authentication succeeds, the session will be created and maintained through a set of cookies in the userβs browser.
Each subsequent request will not contain credentials, but rather a unique cookie that identifies the session. To support login sessions, Passport will serialize and deserialize user instances in and out of the session.
In the code you wrote for the session, only the user ID is serialized. Upon receipt of subsequent requests, this identifier is used to search for the user who will be restored before req.user .
To give developers freedom for the user depending on which database they want, no matter what data they want to serialize, they can do it their own way, the logic of serialization and deserialization remains for us.
NarendraSoni
source share