# restify-ensure-login

> Login session ensuring middleware for Restify.

Latest version **2.0.2** (published 2016-01-09) · 0 weekly downloads

## Install

```sh
npm install restify-ensure-login
pnpm add restify-ensure-login
yarn add restify-ensure-login
bun add restify-ensure-login
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2016-01-09 |
| First published | 2015-07-21 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.10.0 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Jared Hanson |
| Maintainers | jameswomack, kmudrick |
| Keywords | restify, auth, authn, authentication, login, session, passport |

## Links

- npm: https://www.npmjs.com/package/restify-ensure-login
- Repository: https://github.com/jameswomack/restify-ensure-login
- Homepage: https://github.com/jameswomack/restify-ensure-login#readme
- Issues: http://github.com/jameswomack/restify-ensure-login/issues
- npm.io page: https://npm.io/package/restify-ensure-login

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads

## Recent versions

- 2.0.2 (latest) — 2016-01-09
- 2.0.1 — 2016-01-08
- 2.0.0 — 2015-11-23
- 1.0.0 — 2015-07-21

## README

# restify-ensure-login

This middleware ensures that a user is logged in.  If a request is received that
is unauthenticated, the request will be redirected to a login page.  The URL
will be saved in the session, so the user can be conveniently returned to the
page that was originally requested.

If the request appears to be an ajax request (XHR), a 401 status code will be sent
instead of redirecting to login.  This is detected by looking for an X-Requested-With
header, or for query string parameter called ajax with a value of 'true'.

## Install

    $ npm install restify-ensure-login

## Usage

#### Ensure Authentication

In this example, an application has a settings page where preferences can be
configured.  A user must be logged in before accessing this page.

    app.get('/settings',
      ensureLoggedIn('/login'),
      function(req, res) {
        res.render('settings', { user: req.user });
      });

If a user is not logged in when attempting to access this page, the request will
be redirected to `/login` and the original request URL (`/settings`) will be
saved to the session at `req.session.returnTo`.

#### Log In and Return To

This middleware integrates seamlessly with [Passport Restify](https://github.com/jameswomack/passport-restify).
Simply mount Passport's `authenticate()` middleware at the login route.

    app.get('/login', function(req, res) {
      res.render('login');
    });

    app.post('/login', passport.authenticate('local', { successReturnToOrRedirect: '/', failureRedirect: '/login' }));

Upon log in, Passport will notice the `returnTo` URL saved in the session and
redirect the user back to `/settings`.

#### Step By Step

If the user is not logged in, the sequence of requests and responses that take
place during this process can be confusing.  Here is a step-by-step overview of
what happens:

1. User navigates to `GET /settings`
    - Middleware sets `session.returnTo` to `/settings`
    - Middleware redirects to `/login`
2. User's browser follows redirect to `GET /login`
    - Application renders a login form (or, alternatively, offers SSO)
3. User submits credentials to `POST /login`
    - Application verifies credentials
    - Passport reads `session.returnTo` and redirects to `/settings`
4. User's browser follows redirect to `GET /settings`
    - Now authenticated, application renders settings page

## Tests

    $ npm install --dev
    $ make test

[![Build Status](https://secure.travis-ci.org/jameswomack/restify-ensure-login.png)](http://travis-ci.org/jameswomack/restify-ensure-login)

## Credits

  - [Jared Hanson](http://github.com/jaredhanson)

## License

[The MIT License](http://opensource.org/licenses/MIT)

Copyright (c) 2012-2013 Jared Hanson <[http://jaredhanson.net/](http://jaredhanson.net/)>

---
_Source: https://npm.io/package/restify-ensure-login · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
