npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cookie-auth

v2.4.2

Published

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

Downloads

15

Readme

cookie-auth

Build Status dat

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. 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 to do HTTP Basic Authentication:

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)
})