{"id":23999016,"url":"https://github.com/jaredhanson/connect-ensure-login","last_synced_at":"2025-04-11T22:30:08.648Z","repository":{"id":3757929,"uuid":"4833866","full_name":"jaredhanson/connect-ensure-login","owner":"jaredhanson","description":"Login session ensuring middleware for Connect and Express.","archived":false,"fork":false,"pushed_at":"2022-09-17T01:53:33.000Z","size":189,"stargazers_count":468,"open_issues_count":22,"forks_count":61,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-03T05:09:38.315Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jaredhanson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-06-29T15:27:46.000Z","updated_at":"2024-12-08T05:25:20.000Z","dependencies_parsed_at":"2022-07-12T15:00:52.104Z","dependency_job_id":null,"html_url":"https://github.com/jaredhanson/connect-ensure-login","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaredhanson%2Fconnect-ensure-login","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaredhanson%2Fconnect-ensure-login/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaredhanson%2Fconnect-ensure-login/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaredhanson%2Fconnect-ensure-login/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jaredhanson","download_url":"https://codeload.github.com/jaredhanson/connect-ensure-login/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248489376,"owners_count":21112559,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-07T23:27:21.568Z","updated_at":"2025-04-11T22:30:08.615Z","avatar_url":"https://github.com/jaredhanson.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# connect-ensure-login\n\nThis middleware ensures that a user is logged in.  If a request is received that\nis unauthenticated, the request will be redirected to a login page.  The URL\nwill be saved in the session, so the user can be conveniently returned to the\npage that was originally requested.\n\n## Install\n\n    $ npm install connect-ensure-login\n\n## Usage\n\n#### Ensure Authentication\n\nIn this example, an application has a settings page where preferences can be\nconfigured.  A user must be logged in before accessing this page.\n\n    app.get('/settings',\n      ensureLoggedIn('/login'),\n      function(req, res) {\n        res.render('settings', { user: req.user });\n      });\n      \nIf a user is not logged in when attempting to access this page, the request will\nbe redirected to `/login` and the original request URL (`/settings`) will be\nsaved to the session at `req.session.returnTo`.\n\n#### Log In and Return To\n\nThis middleware integrates seamlessly with [Passport](http://passportjs.org/).\nSimply mount Passport's `authenticate()` middleware at the login route.\n\n    app.get('/login', function(req, res) {\n      res.render('login');\n    });\n\n    app.post('/login', passport.authenticate('local', { successReturnToOrRedirect: '/', failureRedirect: '/login' }));\n    \nUpon log in, Passport will notice the `returnTo` URL saved in the session and\nredirect the user back to `/settings`.\n\n#### Step By Step\n\nIf the user is not logged in, the sequence of requests and responses that take\nplace during this process can be confusing.  Here is a step-by-step overview of\nwhat happens:\n\n1. User navigates to `GET /settings`\n    - Middleware sets `session.returnTo` to `/settings`\n    - Middleware redirects to `/login`\n2. User's browser follows redirect to `GET /login`\n    - Application renders a login form (or, alternatively, offers SSO)\n3. User submits credentials to `POST /login`\n    - Application verifies credentials\n    - Passport reads `session.returnTo` and redirects to `/settings`\n4. User's browser follows redirect to `GET /settings`\n    - Now authenticated, application renders settings page\n\n## Tests\n\n    $ npm install --dev\n    $ make test\n\n[![Build Status](https://secure.travis-ci.org/jaredhanson/connect-ensure-login.png)](http://travis-ci.org/jaredhanson/connect-ensure-login)\n\n## Credits\n\n  - [Jared Hanson](http://github.com/jaredhanson)\n\n## License\n\n[The MIT License](http://opensource.org/licenses/MIT)\n\nCopyright (c) 2012-2013 Jared Hanson \u003c[http://jaredhanson.net/](http://jaredhanson.net/)\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaredhanson%2Fconnect-ensure-login","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjaredhanson%2Fconnect-ensure-login","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaredhanson%2Fconnect-ensure-login/lists"}