Skip to content

Commit

Permalink
feat: change .sign to standard async callback
Browse files Browse the repository at this point in the history
- subscribe error event
- change the first argument of the callback as error
  • Loading branch information
popomore committed Apr 27, 2016
1 parent c548032 commit 50873c7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ JWT.sign = function(payload, secretOrPrivateKey, options, callback) {
header: header,
privateKey: secretOrPrivateKey,
payload: JSON.stringify(payload)
}).on('done', callback);
})
.on('error', callback)
.on('done', function(signature) {
callback(null, signature);
});
} else {
return jws.sign({header: header, payload: payload, secret: secretOrPrivateKey, encoding: encoding});
}
Expand Down
9 changes: 8 additions & 1 deletion test/async_sign.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ describe('signing a token asynchronously', function() {
var syncToken = jwt.sign({ foo: 'bar' }, secret, { algorithm: 'HS256' });

it('should return the same result as singing synchronously', function(done) {
jwt.sign({ foo: 'bar' }, secret, { algorithm: 'HS256' }, function (asyncToken) {
jwt.sign({ foo: 'bar' }, secret, { algorithm: 'HS256' }, function (err, asyncToken) {
expect(asyncToken).to.be.a('string');
expect(asyncToken.split('.')).to.have.length(3);
expect(asyncToken).to.equal(syncToken);
done();
});
});

it('should throw error', function(done) {
jwt.sign({ foo: 'bar' }, secret, { algorithm: 'HS2561' }, function (err) {
expect(err).to.be.ok();
done();
});
});
});
});

0 comments on commit 50873c7

Please sign in to comment.