township
v2.0.0
Published
route handlers to use in your REST API to handle user management
Downloads
8
Readme
township
route handlers to use in your REST API to handle user management
quick start
var township = require('township')
var db = require('memdb') // can be any levelup e.g. level-party or level
var config = require('./your-config')
var ship = township(db, config)
// now you can use `ship` to handle (req, res) route handlers
township
just provides route handler functions so you can integrate auth routes into your web server of choice.
here's an example using the require('appa')
REST server module
var memdb = require('memdb')
var createAppa = require('appa')
var township = require('township')
var config = require('./your-config')
var app = createAppa()
var db = memdb()
var ship = township(db, config)
app.on('/register', function (req, res, ctx) {
// appa provides `ctx` for us in the way we want out of the box
ship.register(req, res, ctx, function (err, respCode, data) {
if (err) return app.error(res, respCode, err.message)
app.send(res, respCode, data)
})
})
see also test-server.js
API
var township = require('township')
returns a constructor you can use to make multiple instances
var ship = township(db, config)
creates a new instance
db
should be a levelup instance
config
properties:
secret
(String) - used with township-tokenemail
(Object) - used to send emails with postmarkemail.fromEmail
(String) - from addressemail.postmarkAPIKey
(String)
ship.verify(req, cb)
given a request
, decodes and verifies the token in the authorization header and calls cb
with the result
pass req
from your http server. the request is expected to have an Authorization: Bearer <token>
header.
cb
will be called with (error, decodedToken, rawToken)
.
error
will be called if the token is missing from the request or had a problem being verified
decodedToken
is a JS object with the result of jwt.verify
.
rawToken
is a string containing the encoded token value received from the request header
ship.register(req, res, ctx, cb)
registers a new user. pass req
, res
from your http server.
ctx
should be an object with:
body
(Object) - the POST JSON body as a parsed Objectbody.email
(String)body.password
(String)
cb
is called with (err, newToken)
ship.login(req, res, ctx, cb)
returns a token for an existing user
ctx
should be an object with:
body
(Object) - the POST JSON body as a parsed Objectbody.email
(String)body.password
(String)
cb
is called with (err, token)
ship.updatePassword(req, res, ctx, cb)
changes a users password, invalidates old token and issues new token
ctx
should be an object with:
body
(Object) - the POST JSON body as a parsed Objectbody.email
(String)body.password
(String)body.newPassword
(String)
cb
is called with (err, newToken)