Failed to serialize user into session 🔐 Authentication

Passport.js: Error: Failed to serialize user into session

Passport logged a user in but there’s no serializeUser function (or it didn’t call done), so the session can’t store the user.

Seen on: REST API

Meaning

Session-based Passport setups need passport.serializeUser and deserializeUser, and the session middleware before passport.session(). Calling done with undefined also fails.

Common causes

  • serializeUser not defined
  • done() not called / called with undefined id
  • Middleware order wrong (session after passport)
  • Using sessions when session: false was intended

⚡ Quick fix

  1. Define serializeUser/deserializeUser
  2. Order: express-session → passport.initialize() → passport.session()
  3. Use { session: false } for token-based APIs

Detailed fix by platform

JavaScript

  1. javascript
    passport.serializeUser((user, done) => done(null, user.id));
    passport.deserializeUser(async (id, done) => done(null, await User.findById(id)));

How to diagnose

  1. Functions — Defined?
  2. Middleware — Order
  3. Mode — Sessions or JWT?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.