Permalink
Browse files

Add save_access_token event to allow saving the access token.

For some reason, people might want to save the generated access token in
a database, so the save_access_token event is now emitted. It is
optional.

Closes #10
  • Loading branch information...
1 parent 706185b commit 7acc5ed87061cc9717214d6af75339fbf7cb725b @ammmir committed Jul 6, 2012
Showing with 16 additions and 1 deletion.
  1. +5 −0 examples/simple.js
  2. +11 −1 index.js
View
@@ -67,6 +67,11 @@ myOAP.on('create_access_token', function(user_id, client_id, next) {
next(data);
});
+// (optional) do something with the generated access token
+myOAP.on('save_access_token', function(user_id, client_id, access_token) {
+ console.log('saving access token %s for user_id=%s client_id=%s', access_token, user_id, client_id);
+});
+
// an access token was received in a URL query string parameter or HTTP header
myOAP.on('access_token', function(req, token, next) {
var TOKEN_TTL = 10 * 60 * 1000; // 10 minutes
View
@@ -116,7 +116,12 @@ OAuth2Provider.prototype.oauth = function() {
}
self.emit('create_access_token', user_id, client_id, function(extra_data) {
- url += querystring.stringify(self.generateAccessToken(user_id, client_id, extra_data));
+ var atok = self.generateAccessToken(user_id, client_id, extra_data);
+
+ if(self.listeners('save_access_token').length > 0)
+ self.emit('save_access_token', user_id, client_id, atok);
+
+ url += querystring.stringify(atok);
res.writeHead(303, {Location: url});
res.end();
@@ -161,6 +166,11 @@ OAuth2Provider.prototype.oauth = function() {
res.writeHead(200, {'Content-type': 'application/json'});
self.emit('create_access_token', user_id, client_id, function(extra_data) {
+ var atok = self.generateAccessToken(user_id, client_id, extra_data);
+
+ if(self.listeners('save_access_token').length > 0)
+ self.emit('save_access_token', user_id, client_id, atok);
+
res.end(JSON.stringify(self.generateAccessToken(user_id, client_id, extra_data)));
});

0 comments on commit 7acc5ed

Please sign in to comment.