Create an accounts object
Parameters
db
Object – instance of a level db. See https://npmjs.com/levelconfig
Object – configuration object
Examples
var townshipAccounts = require('township-accounts')
var level = require('level')
var db = level('db')
var accounts = townshipAccounts(db, {
secret: 'not a secret'
})
Create an account
Parameters
options
Object – email address that corresponds to an accountcallback
Function – callback function called witherror
andaccountData
arguments
Examples
var creds = {
email: 'user@example.com',
password: 'notverysecret',
scopes: ['example:read']
}
accounts.register(creds, function (err, account) {
if (err) return console.log(err)
console.log(account.key, account.token)
})
Log in to an account
Parameters
options
Object – email address that corresponds to an accountcallback
Function – callback function called witherror
andaccount
arguments
Examples
var creds = {
email: 'user@example.com',
password: 'notverysecret'
}
accounts.login(creds, function (err, account) {
if (err) return console.log(err)
console.log(account.key, account.token)
})
Log out of an account
Parameters
token
String – the active token for an accountcallback
Function – callback function called with anerror
argument
Examples
accounts.logout(token, function (err) {
if (err) return console.log(err)
})
Destroy an account
Parameters
key
String – the key for an accountcallback
Function – callback function called with anerror
argument
Examples
accounts.destroy(token, function (err) {
if (err) return console.log(err)
})
Find an account by email
Parameters
email
String – email address that corresponds to an accountcallback
Function – callback function called witherror
andaccountData
arguments
Examples
accounts.findByEmail('user@example.com', function (err, accountData) {
if (err) return console.log(err)
console.log(accountData)
})
Update password of an account
Parameters
options
Object – email address that corresponds to an accountcallback
Function – callback function called witherror
andaccount
arguments
Examples
var creds = {
email: 'user@example.com',
password: 'notverysecret',
newPassword: 'still not very secret',
token: token
}
accounts.updatePassword(creds, function (err, account) {
if (err) return console.log(err)
console.log(account.key, account.token)
})
Authorize account using access scopes
Parameters
accountKey
String – the key for an accountscopes
Array – the scopes your are checking for in the account's access permissionscallback
Function – callback function called with anerror
argument
Examples
accounts.authorize(key, ['example:read'], function (err) {
if (err) return console.log(err)
})
Authorize account using access scopes via their email
Parameters
email
String – the email address associated with an accountscopes
Array – the scopes your are checking for in the account's access permissionscallback
Function – callback function called with anerror
argument
Examples
accounts.authorizeByEmail('user@example.com', ['example:read'], function (err) {
if (err) return console.log(err)
})
Update the access scopes of an account
Parameters
key
String – the key associated with an accountscopes
Array – the new scopes of the account. note that this completely replaces the old scopes arraycallback
Function – callback function called with anerror
argument
Examples
accounts.updateScopes(key, ['example:read'], function (err) {
if (err) return console.log(err)
})
verify that a token is valid
Parameters
token
String – the JWT created when registering or logging incallback
Function – callback function called witherror
andaccountData
arguments
Examples
accounts.verifyToken(token, function (err, account) {
if (err) return console.log(err)
})