route handlers to use in your REST API to handle user management
var Township = require('township-accounts-api') var db = require('memdb') // can be any levelup e.g. level-party or level var config = require('./your-config') var ship = new 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-accounts-api') var config = require('./your-config') var app = createAppa() var db = memdb() var ship = new 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
returns a constructor you can use to make multiple instances
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)
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
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)
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)
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)