https://github.com/doowb/accountdown-token
Token authentication for accountdown
https://github.com/doowb/accountdown-token
Last synced: 5 months ago
JSON representation
Token authentication for accountdown
- Host: GitHub
- URL: https://github.com/doowb/accountdown-token
- Owner: doowb
- License: mit
- Created: 2015-02-03T17:10:28.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-10-16T02:54:16.000Z (almost 11 years ago)
- Last Synced: 2025-10-10T12:19:09.724Z (10 months ago)
- Language: JavaScript
- Size: 156 KB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# accountdown-token [](http://badge.fury.io/js/accountdown-token)
> Token authentication for accountdown
Install with [npm](https://www.npmjs.com/)
```sh
$ npm i accountdown-token --save
```
## Running tests
Install dev dependencies:
```sh
$ npm i -d && npm test
```
## Usage
```js
var Token = require('accountdown-token');
var accountdown = require('accountdown');
var level = require('level');
var db = level('tmp/users.db');
var users = accountdown(db, {
login: {
token: Token
}
});
var username = 'doowb';
var opts = {
login: { token: { username: username, token: 'foobarbaz' } },
value: { name: 'Brian' }
};
users.create(username, opts, function (err) {
if (err) return console.log('Error creating user', err);
users.verify('token', opts.login.token, function (err, ok, id) {
if (err) return console.log('Error', err);
if (ok === false) return console.log('Invalid token for ' + id);
console.log('Valid token for ' + id);
});
});
```
## API
### [.verify](index.js#L47)
Verify that the token is correct and still valid (e.g. not expired)
**Params**
* `creds` **{Object}**: Credentials object containing a `username` and `token` to validate.
* `cb` **{Function}**: Callback that takes `err, ok, id`
**Example**
```js
login.verify({ username: 'doowb', token: 'XXXXXXXXXXXX' }, function (err, ok, id) {
if (err) return console.log('Error validating token');
if (ok === true) return console.log('valid token for ' + id);
console.log('Invalid token for ' + id);
});
```
### [.create](index.js#L97)
Create a new user token for the given id and credentials.
**Params**
* `id` **{String}**: Identifier of the account being created.
* `creds` **{Object}**: Credentials object used for creating the token.
* `returns` **{Array}**: Array of rows to be added to the [accountdown](https://github.com/substack/accountdown)database
**Example**
```js
login.create('doowb', { username: 'doowb' });
```
## Related projects
* [accountdown](https://github.com/substack/accountdown): persistent accounts backed to leveldb
* [level](https://github.com/Level/level): Fast & simple storage - a Node.js-style LevelDB wrapper (a convenience package bundling LevelUP &… [more](https://github.com/Level/level)
* [levelup](https://github.com/level/levelup): Fast & simple storage - a Node.js-style LevelDB wrapper
* [leveldown](https://github.com/level/leveldown): A Node.js LevelDB binding, primary backend for LevelUP
## Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/doowb/accountdown-token/issues)
## Author
**Brian Woodward**
+ [github/doowb](https://github.com/doowb)
+ [twitter/doowb](http://twitter.com/doowb)
## License
Copyright © 2015 Brian Woodward
Released under the MIT license.
***
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on July 23, 2015._