Skip to content

Commit

Permalink
Add save_access_token event to allow saving the access token.
Browse files Browse the repository at this point in the history
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
ammmir committed Jul 6, 2012
1 parent 706185b commit 7acc5ed
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions examples/simple.js
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion index.js
Expand Up @@ -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();
Expand Down Expand Up @@ -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)));
});

Expand Down

0 comments on commit 7acc5ed

Please sign in to comment.