# cookie-auth

> A simple authentication handler that uses cookies. Great for cli or rest clients.

Latest version **2.4.2** (published 2015-04-05) · BSD license · 0 weekly downloads

## Install

```sh
npm install cookie-auth
pnpm add cookie-auth
yarn add cookie-auth
bun add cookie-auth
```

## 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.4.2 |
| Published | 2015-04-05 |
| First published | 2014-11-04 |
| Weekly downloads | 0 |
| License | BSD |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 11 |
| Author | karissa |
| Maintainers | karissa, maxogden |
| Keywords | authentication, http, cookies, cookie, sessions |

## Links

- npm: https://www.npmjs.com/package/cookie-auth
- Repository: https://github.com/karissa/cookie-auth
- Issues: https://github.com/karissa/cookie-auth/issues
- npm.io page: https://npm.io/package/cookie-auth

## Dependencies (3)

- [debug](https://npm.io/package/debug.md) ^2.1.0
- [memdb](https://npm.io/package/memdb.md) ^0.2.0
- [cookie-cutter](https://npm.io/package/cookie-cutter.md) ^0.1.1

## Alternatives

- [@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
- [escher-request](https://npm.io/package/escher-request.md) — 545 weekly downloads

## Recent versions

- 2.4.2 (latest) — 2015-04-05
- 2.4.1 — 2015-01-07
- 2.4.0 — 2015-01-07
- 2.3.0 — 2015-01-06
- 2.2.1 — 2015-01-06
- 2.2.0 — 2015-01-01
- 2.1.0 — 2015-01-01
- 2.0.0 — 2014-12-30
- 1.0.0 — 2014-11-18
- 0.0.2 — 2014-11-04
- 0.0.1 — 2014-11-04

## README

cookie-auth
===========

[![Build Status](https://travis-ci.org/karissa/cookie-auth.svg?branch=master)](https://travis-ci.org/karissa/cookie-auth)
![dat](http://img.shields.io/badge/Development%20sponsored%20by-dat-green.svg?style=flat)

This module does one thing very simply, and that's let your node process authenticate using cookies. Great for CLI/REST clients.

## installation

```
npm install cookie-auth
```

## API

### `var auth = require('cookie-auth')(opts)`

`opts` properties:

- `authenticator` - **required** - the authentication handler (see below)
- `name` - the name of the session token key in the cookies (defaults to `session`)
- `sessions` - a levelup instance to store sessions in. defaults to [`memdb`](http://npmjs.org/memdb). note that expiry/ttl is not handled by default

You **must** pass in your own 'authenticator' function that looks like this:

```
function authenticator(req, res, cb) {
  // do auth with req, res and call cb when done
  
  doSomeAuthThing(req, res, function(err) {
    // e.g. if not authorized
    cb(err)
  
    // otherwise, if authorized
    cb()
  })
}
```

### `auth.login(res, cb)`

creates a new session and sets a cookie on `res`, and calls `cb` when done with `(err, data)` where `err` is any error from the session store `.put` and `data` is the session response data (see below).

### `auth.logout(req, res, cb)`

deletes the session from the session store (using `auth.delete`) and sets a set-cookie to delete the cookie on `res`. calls the optional `cb` when done with no arguments

### `auth.delete(req, cb)`

deletes the session from the session store calls `cb` when done with `(err)` if an error occured with the `store.del` call

### `auth.handle(req, res, cb)`

looks up the session for `req` and if it is already valid calls `cb`. If no session exists then the `authenticator` will be attempted. If authentication is successful then `auth.login` gets called with `cb`.

### response data

for valid sessions the following data is returned to the `cb` as the second argument (for both `login` and `handle`):

`{session: <session-id-string>, created: <created-at-ISO-8601-date-string>}`

e.g.

`{session: "b7c91436a316ef5b189467b9898d21e6", created: "2015-01-01T21:59:04.844Z"}`

in the case of an error `cb` will be called with the `Error` as the first argument.

## Usage

Here is an example of using the [`basic` module from npm](http://npmjs.org/basic) to do HTTP Basic Authentication:

```javascript
var Auth = require('cookie-auth')
var basic = require('basic')

// set up server and router code...

var basicAuthenticator = basic(function (user, pass, callback) {
  if (user === 'test' && pass === 'pass') return callback(null)
  callback(new Error("Access Denied"))
})
  
var auth = Auth({
  name: 'my-application',
  authenticator: basicAuthenticator
})

router.addRoute('/login', function (req, res) {
  var self = this
  auth.handle(req, res, function(err, session) {
    if (err) return auth.logout(req, res)
    res.end(JSON.stringify(session))
  })
});

router.addRoute('/logout', function (req, res) {
  return auth.logout(req, res)
})
```

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