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

Latest commit

 

History

History
223 lines (154 loc) · 8.02 KB

api.md

File metadata and controls

223 lines (154 loc) · 8.02 KB

townshipAccounts

Create an accounts object

Parameters

Examples

var townshipAccounts = require('township-accounts')
var level = require('level')
var db = level('db')

var accounts = townshipAccounts(db, {
  secret: 'not a secret'
})

register

Create an account

Parameters

  • options Object – email address that corresponds to an account
    • options.email String – email address that corresponds to an account
    • options.password String – password that corresponds to an account
    • options.scopes Array – access scopes for an account
  • callback Function – callback function called with error and accountData 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)
})

login

Log in to an account

Parameters

  • options Object – email address that corresponds to an account
    • options.email String – email address that corresponds to an account
    • options.password String – password that corresponds to an account
  • callback Function – callback function called with error and account 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)
})

logout

Log out of an account

Parameters

  • token String – the active token for an account
  • callback Function – callback function called with an error argument

Examples

accounts.logout(token, function (err) {
  if (err) return console.log(err)
})

logout

Destroy an account

Parameters

  • key String – the key for an account
  • callback Function – callback function called with an error argument

Examples

accounts.destroy(token, function (err) {
  if (err) return console.log(err)
})

findbyEmail

Find an account by email

Parameters

  • email String – email address that corresponds to an account
  • callback Function – callback function called with error and accountData arguments

Examples

accounts.findByEmail('user@example.com', function (err, accountData) {
  if (err) return console.log(err)
  console.log(accountData)
})

updatePassword

Update password of an account

Parameters

  • options Object – email address that corresponds to an account
    • options.email String – email address that corresponds to an account
    • options.password String – password that corresponds to an account
    • options.newPassword String – new password for an account
  • callback Function – callback function called with error and account 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

Authorize account using access scopes

Parameters

  • accountKey String – the key for an account
  • scopes Array – the scopes your are checking for in the account's access permissions
  • callback Function – callback function called with an error argument

Examples

accounts.authorize(key, ['example:read'], function (err) {
  if (err) return console.log(err)
})

authorizeByEmail

Authorize account using access scopes via their email

Parameters

  • email String – the email address associated with an account
  • scopes Array – the scopes your are checking for in the account's access permissions
  • callback Function – callback function called with an error argument

Examples

accounts.authorizeByEmail('user@example.com', ['example:read'], function (err) {
  if (err) return console.log(err)
})

updateScopes

Update the access scopes of an account

Parameters

  • key String – the key associated with an account
  • scopes Array – the new scopes of the account. note that this completely replaces the old scopes array
  • callback Function – callback function called with an error argument

Examples

accounts.updateScopes(key, ['example:read'], function (err) {
  if (err) return console.log(err)
})

verifyToken

verify that a token is valid

Parameters

  • token String – the JWT created when registering or logging in
  • callback Function – callback function called with error and accountData arguments

Examples

accounts.verifyToken(token, function (err, account) {
  if (err) return console.log(err)
})