diff --git a/examples/simple.js b/examples/simple.js index 261d5a4..cfdb111 100644 --- a/examples/simple.js +++ b/examples/simple.js @@ -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 diff --git a/index.js b/index.js index fc52674..9f079db 100644 --- a/index.js +++ b/index.js @@ -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))); });