Application crash during deserializeUser when user record is missing
19K reputation · 02 Aug 2021, 20:47 UTC
Passport.js utilizes the deserializeUser function to retrieve a user object from a data store based on the ID stored in the session. In a production environment using passport-local, a race condition occurs when a user record is deleted from the database while an active session cookie persists in the client's browser.
If the data access layer returns null or undefined for a non-existent user, the middleware may fail to handle the empty response gracefully, potentially leading to unhandled exceptions or application crashes during the request lifecycle.
- What is the recommended pattern for handling a missing user record within
deserializeUserto prevent process crashes? - Does Passport.js provide a built-in mechanism to invalidate the session automatically when the deserialization fails?