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 🙏

© 2026 – Pkg Stats / Ryan Hefner

csurf-login-token

v1.0.1

Published

CSRF token from login token middleware

Readme

csurf with login tokens

Node.js CSRF protection middleware.

Requires cookie-parser to be initialized first.

This module is based off of csurf and uses an almost identical API, however it uses an existing login token instead of creating a new token and cookie. The security implications of doing this are discussed here.

Installation

$ npm install csurf-login-token --save

API

const csurf = require('csurf-login-token');

csurf(cookieName[, options])

cookieName

The name of the cookie where the login token is stored. There are many online tutorials on how to generate this securely, please do so.

Options

The csurf function takes an optional options object that may contain any of the following keys:

ignoreMethods

An array of the methods for which CSRF token checking will disabled. Defaults to ['GET', 'HEAD', 'OPTIONS'].

value

Provide a function that the middleware will invoke to read the token from the request for validation. The function is called as value(req) and is expected to return the token as a string.

The default value is a function that reads the token from the following locations, in order:

  • req.body._csrf - typically generated by the body-parser module.
  • req.query._csrf - a built-in from Express.js to read from the URL query string.
  • req.headers['csrf-token'] - the CSRF-Token HTTP request header.
  • req.headers['xsrf-token'] - the XSRF-Token HTTP request header.
  • req.headers['x-csrf-token'] - the X-CSRF-Token HTTP request header.
  • req.headers['x-xsrf-token'] - the X-XSRF-Token HTTP request header.

Examples, Ignoring Routes, Custom error handling

Since the API is identical, please check the examples in the CSURF repo.

How it works

The CSRF token is generated by hashing the login cookie using the SHA3-512 algorithm. As long as the login token is long enough and generated using a cryptographically secure pseudorandom generator, it's hash will be too. This is perfectly safe because by definition, hashes are one way functions so the login token cannot be found in a reasonable time period. For more information please see this post.

Note: If there is no login token present the same error (EBADCSRFTOKEN) will be thrown as if the validation failed.

Benefits

  • All the benefits of cookie based CSRF tokens
  • Prevents having to store an extra cookie on the client reducing network traffic on every request
  • If the login token is indeed unique per session (even between the same users) then each csrf token will be unique per session allowing for multiple tabs to be open without causing issues