Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
/ township-token Public archive

Create & decode JWT tokens with township-auth/township-access data

License

Notifications You must be signed in to change notification settings

township/township-token

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

township-token

Create JWT tokens using township auth & access data.

Install

npm i -g township-token

Examples

Basic example:

var level = require('level')
var townshipToken = require('township-token')

var db = level('./db')
var tokens = townshipToken(db, { secret: process.env.TOWNSHIP_SECRET })

// create a token
var token = tokens.sign({
  auth: { basic: { key: 'example', email: 'email@example.com' } },
  access: { scopes: ['site:read', 'site:edit'] },
  data: { arbitrary: 'data' }
})

// verify and decode a token
tokens.verify(token, function (err, data) {
  // use the data from the token
})

Full example with township-auth & township-access:

var level = require('level')
var townshipToken = require('township-token')
var townshipAccess = require('township-access')
var townshipAuth = require('township-auth')
var basic = require('township-auth/basic')

var db = level('db')
var tokens = townshipToken(db, { secret: process.env.TOWNSHIP_SECRET })
var access = townshipAccess(db)
var auth = townshipAuth(db, {
  providers: { basic: basic }
})

var creds = { basic: { email: 'hi@example.com', password: 'oops' } }

// create an auth record
auth.create(creds, function (err, authData) {

  // create an access record
  access.create(account.key, ['site:read'], function (err, accessData) {

    // create a token for a client to use
    var token = tokens.sign({
      auth: authData,
      access: accessData
    })

    // verify the token when received from a client
    tokens.verify(token, function (err, data) {
      // use the data from the token
    })
  })
})

API

Read the API docs for this module in docs/api.md

See also

License

MIT

About

Create & decode JWT tokens with township-auth/township-access data

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •