Skip to content

Instantly share code, notes, and snippets.

@modulitos
Last active August 29, 2015 14:20
Show Gist options
  • Save modulitos/048cee99d28144aade14 to your computer and use it in GitHub Desktop.
Save modulitos/048cee99d28144aade14 to your computer and use it in GitHub Desktop.
Ideas for a module that automatically handles account generation/updates from forms

forms2accounts

This is a set of ideas for how an forms2accounts module could work that uses accountdown and is meant to facilitate the creation and updates of accounts from a form. Primary usage would be on the server.

API

var forms2accounts = require('forms2accounts')

or some similar package name. forms2accounts is available, though

var accounts = forms2accounts(accountdown, options)

  • server.accountdown: an accountdown instance for updating and storing accounts
  • account: An associative array defining the schema of the accounts. Each field is mapped to either a boolean describing whether the field is required, or a default value when the field is not provided. If the default value is a boolean, then it is listed in quotes. Here is an example that uses the accountdown-basic module as the credentials plugin while supplying a customized value attribute with three required fields (email, username, and uuid), one non-required field (favorite-food) and two optional fields with default values (admin and color):
  var account = {
    login: {
      basic: {
        username: true,
        password: true
      }
    },
    value: {
      admin: 'false',
      color: randomColor(),
      favorite-food: false,
      email: true,
      username: true,
      uuid: true
    }
  };
  • options: TBD

accounts.create(req, res, callback)

  • req, res: Parses the req and res objects using the body's body/form module, and creates an account

Internally, all accounts are created with the key generated from uuid, version 1.

  • callback provides error argument

accounts.update(req, res, uuid, callback)

  • req, res: Parses the req and res objects using the body's body/form module, and creates an account

  • callback provides an errorargument.

accounts.remove(uuid, callback)

  • uuid: string key that identifies the account that will be removed

Internally, this function is just a convenience wrapper for the accountdown.remove function

  • callback provides an errorargument.

accounts.list()

Returns a stream of all accounts

Internally, this function is just a convenience wrapper for the accountdown.list function

accounts.get(uuid, callback)

  • uuid: string key that identifies the account that will be retrieved

Internally, this function is just a convenience wrapper for the accountdown.get function

  • callback provides error argument
@sethvincent
Copy link

Maybe it really only needs the create and update methods? Then we would use the accountdown instance like normal for things like list, remove, & get.

@sethvincent
Copy link

So it seems like the usage example would be something like this:

A file that defines some routes:

var router = // whatever router we use
var accounts = require('forms2accounts')

module.exports = function (server) {
  var accountsHandler = accounts(server.accountdown)

  router.on('/accounts', function (req, res) {
    if (req.method === 'GET') {
      /* respond with list of accounts */
    }

    if (req.method === 'POST')  {
      accountsHandler.create(req, res, function (error) { 
        /* respond if error */ }
      })
    }
  })

  return router
}

It would be cool if the callback was optional. Maybe the error response could be handled in some default way, and we pass the callback only if we need to do something special

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment