Connect-Ensure-Login is a middleware designed for Connect-compatible frameworks like Express.js, primarily used to ensure a user is authenticated before accessing protected routes. If an unauthenticated request is received, the middleware redirects the user to a specified login page and stores the original requested URL in the session (via `req.session.returnTo`). After successful authentication, the application can then redirect the user back to their intended destination. The package's current stable version, 0.1.1, was released in 2013, making it over a decade old and effectively abandoned. It has no active release cadence or maintenance. Its key differentiator was its straightforward integration with Passport.js for handling common authentication flows, but its age means it lacks modern features, security updates, and compatibility with contemporary Node.js and web development practices, making it unsuitable for new projects.
npm install connect-ensure-loginVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to set up an Express application with `express-session`, `passport`, and `connect-ensure-login` to protect a route, redirect unauthenticated users to a login page, and then return them to the original protected route after successful login.
Migrate to a modern authentication solution, or reimplement the core logic directly with updated middleware for current Express and Passport versions. For basic login ensure, `passport.authenticate('local', { failureRedirect: '/login', keepSessionInfo: true })` combined with manual `req.session.returnTo` handling can achieve similar results.Ensure your project is configured for CommonJS, or use dynamic `import()` if absolutely necessary and you understand the implications of mixing module systems. The best fix is to avoid this package in modern ESM projects.
Avoid using this package in production. If legacy code must run, thoroughly audit its usage and consider isolating the application. Prioritize migration to actively maintained authentication solutions.
Ensure `express-session`, `passport.initialize()`, and `passport.session()` are all correctly configured and placed *before* `connect-ensure-login` in your middleware stack.
This package is CommonJS-only. Convert your file to CommonJS (`.js` without `"type": "module"` in `package.json`) or use dynamic `import()` for the module, if possible, although it's generally better to use modern alternatives.
Ensure `app.use(session(...))` is called *before* `app.use(ensureLoggedIn(...))`.
Ensure `app.use(passport.initialize())` and `app.use(passport.session())` are called *before* `app.use(ensureLoggedIn(...))`.