Skip to content

Commit cf1d46f

Browse files
author
Lars-Magnus Skog
committed
add .putToken()
1 parent 0b0c6d2 commit cf1d46f

File tree

3 files changed

+102
-24
lines changed

3 files changed

+102
-24
lines changed

README.md

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,55 @@ This is the token manager for the http://revisit.link project.
44

55
## Usage
66

7-
var RevisitToken = require('revisit-token');
8-
var rt = new RevisitToken();
9-
10-
// Generate a new token
11-
rt.generate(function (err, tk) {
12-
console.log(tk);
13-
});
14-
15-
// Check if a token still exists
16-
rt.getToken(token, function (err, tk) {
17-
if (err) {
18-
// Token has expired
19-
console.log(err);
20-
}
21-
});
7+
```js
8+
var RevisitToken = require('revisit-token');
9+
var rt = new RevisitToken();
10+
11+
// Generate a new uuid token
12+
t.generate(function (err, tk) {
13+
console.log(tk);
14+
});
15+
16+
// Custom tokens with custom ttl
17+
t.putToken('wooohoo', 56000, function (err, tk) {
18+
console.log(tk);
19+
});
20+
21+
// Check if a token still exists
22+
rt.getToken(token, function (err, tk) {
23+
if (err) {
24+
// Token has expired
25+
console.log(err);
26+
}
27+
});
28+
```
29+
30+
## Api
31+
32+
### `RevisitToken([options])`
33+
34+
Constructor with optional options object, which takes the following properties:
35+
36+
* `db` *(string)* Path to db. Defaults to `'./db-tokens'`.
37+
* `ttl` *(number | string)* TTL in milliseconds. Defaults to 24 hours.
38+
* `frequency` *(number | string)* Interval in milliseconds how often the tokens should be checked. Defaults to 10 seconds.
39+
40+
### `.generate(cb)`
41+
42+
Generates an uuid token with a ttl set to the value set in the constructor. Calls back with `(err, token)`.
43+
44+
### `.putToken(token[, ttl], cb)`
45+
46+
As `generate()` but enables storing custom tokens with a custom ttl. Calls back with `(err, token)`.
47+
48+
* `token` *(string)* Token to store.
49+
* `ttl` *(number | string)* Optional TTL in milliseconds. Defaults to the ttl value set in the constructor.
50+
51+
### `.getToken(token, cb)`
52+
53+
Retrieves a token. Calls back with `(err, token)`. If `err` is set the token has expired.
54+
55+
* `token` *(string)* Token to retrieve.
56+
57+
## License
58+
BSD

index.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ var ttl = require('level-ttl');
77
var TTL_LIMIT = 1000 * 60 * 60 * 24; // 24 hours
88

99
var RevisitToken = function (options) {
10+
if (!(this instanceof RevisitToken)) {
11+
return new RevisitToken(options);
12+
}
13+
1014
if (!options) {
1115
options = {};
1216
}
1317

1418
var dbPath = options.db || './db-tokens';
15-
var limit = parseInt(options.ttl, 10) || TTL_LIMIT;
19+
var defaultTTL = parseInt(options.ttl, 10) || TTL_LIMIT;
1620
var frequency = parseInt(options.frequency, 10) || 10000;
1721

1822
var db = level(dbPath, {
@@ -23,14 +27,20 @@ var RevisitToken = function (options) {
2327
db = ttl(db, { checkFrequency: frequency || 10000 });
2428

2529
this.generate = function (next) {
26-
var token = uuid.v4();
30+
this.putToken(uuid.v4(), next);
31+
};
32+
33+
this.putToken = function (token, ttl, next) {
34+
if (typeof ttl == 'function') {
35+
next = ttl;
36+
ttl = defaultTTL;
37+
}
2738

2839
db.put('token!' + token, token, {
29-
ttl: limit
40+
ttl: ttl
3041
}, function (err) {
3142
if (err) {
32-
next(err);
33-
return;
43+
return next(err);
3444
}
3545

3646
next(null, token);
@@ -40,8 +50,7 @@ var RevisitToken = function (options) {
4050
this.getToken = function (token, next) {
4151
db.get('token!' + token, function (err, token) {
4252
if (err || !token) {
43-
next(new Error('No token found'));
44-
return;
53+
return next(new Error('No token found'));
4554
}
4655

4756
next(null, token);

test/test.index.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var child = require('child_process');
88
var RevisitToken = require('../index');
99
var rt = new RevisitToken({
1010
db: './test/db-token',
11-
ttl: 500,
11+
ttl: 50,
1212
frequency: 1
1313
});
1414

@@ -26,7 +26,39 @@ describe('RevisitToken', function () {
2626
should.exist(err);
2727
done();
2828
});
29-
}, 1500);
29+
}, 100);
3030
});
3131
});
32+
33+
it('should handle custom tokens', function (done) {
34+
var customToken = 'wearetheknightswhosay';
35+
rt.putToken(customToken, function (err, token) {
36+
should.exist(token);
37+
should.equal(token, customToken);
38+
39+
setTimeout(function () {
40+
rt.getToken(token, function (err) {
41+
should.exist(err);
42+
done();
43+
});
44+
}, 100);
45+
});
46+
});
47+
48+
it('should handle custom tokens with custom ttl', function (done) {
49+
var customToken = 'wearenolongertheknightswhosay';
50+
rt.putToken(customToken, 300, function (err, token) {
51+
should.exist(token);
52+
should.equal(token, customToken);
53+
54+
setTimeout(function () {
55+
rt.getToken(customToken, function (err, token) {
56+
should.not.exist(err);
57+
should.equal(token, customToken);
58+
done();
59+
});
60+
}, 100);
61+
});
62+
});
63+
3264
});

0 commit comments

Comments
 (0)