node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »



This (node.js) module associates an authentication procedure with a token generation. As such, all you have to do is to create a TokenAuthentication object, set your authentication criterias, and then, feel free to ask for token generation any time !

you can also set these settings:

  • Do you want the String-encoded token directly or the Buffer Object it-self ?
  • What length (in bytes) for the generated buffer ?
  • Which encoding do you wanna use for String-encoding ? (hex, utf8...?)


You can clone this repo, or install it through NPM:

npm install tokenauthentication

How to use ?

var auth = require("tokenauthentication");
// Define your authentication function 
// As many arguments as needed 
auth.authenticater = function (username, password) {
    return (username === "Tim" && password === "Tom");
// Do some of your stuff here... 
// ... 
// By default, the token return an hex 64 bytes string-encoded token 
// But you can ask for the original Buffer Object: 
// Or also want a longer/smaller buffer ? 
// Be careful, it should be a mutiple of 8 ! 
// Or, maybe an ascii string-encoded instead of hex ? 
// Be sure that the encoding is defined as available 
// For that, see the config/default.yaml configuration file 
// Ask for a token then 
// Don't forget to pass needed arguments 
var token = auth.getToken("Tim", "Tom");

The returned object from the auth.getToken method is as follow:

    "result": true,
    "token": <Your token here>

If the authentication is wrong, then:

    "result": false,
    "token": null