Skip to content

Commit

Permalink
Removed logic disallowing the app from being authorized more than once.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbro-io committed Feb 12, 2014
1 parent 66ddeed commit f40d895
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions web.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,29 +111,29 @@ function authorize()
if(refreshToken)
{
oa.getOAuthAccessToken(refreshToken, {grant_type:'refresh_token', client_id: clientId, client_secret: clientSecret}, function(err, access_token, refresh_token, res){

//lookup settings from database
connect().then(function(){
database.collection(mongoCollectionName).findOne({google_user_id: googleUserId}, function(findError, settings){

var expiresIn = parseInt(res.expires_in);
var accessTokenExpiration = new Date().getTime() + (expiresIn * 1000);

//add refresh token if it is returned
if(refresh_token != undefined) settings.google_refresh_token = refresh_token;

//update access token in database
settings.google_access_token = access_token;
settings.google_access_token_expiration = accessTokenExpiration;

database.collection(mongoCollectionName).save(settings);

console.log('-- access token updated:', access_token);

deferred.resolve(access_token);
});
});

})
}
else
Expand All @@ -153,7 +153,7 @@ function getAccessToken()

database.collection(mongoCollectionName).findOne({google_user_id: googleUserId}, function(findError, settings){
console.log('GOOGLE SETTINGS RESPONSE:', settings, findError);

//check if access token is still valid
var today = new Date();
var currentTime = today.getTime();
Expand All @@ -178,7 +178,7 @@ function getAccessToken()
});
}
});

}, function(error){
deferred.reject(error);
});
Expand All @@ -195,22 +195,13 @@ app.get('/', function(request, response) {
});

app.get('/authorize', function(request, response){

//only allow application to be authorized once
if(refreshToken)
{
response.send({error: 'Application is already authorized. Deauthorization required.'});
}
else
{
oa = new oauth.OAuth2(clientId,
clientSecret,
"https://accounts.google.com/o",
"/oauth2/auth",
"/oauth2/token");

response.redirect(oa.getAuthorizeUrl({scope:scopes, response_type:'code', redirect_uri:baseUrl+'/callback', access_type:'offline',user_id:googleUserId}));
}
});

app.get('/deauthorize', function(request, response){
Expand All @@ -232,23 +223,23 @@ app.get('/callback', function(request, response){
database.collection(mongoCollectionName).findOne({google_user_id: googleUserId}, function(findError, settings){
console.log('--writing access token to database--');
var accessTokenExpiration = new Date().getTime() + (3500 * 1000);

//update access token in database
settings.google_access_token = access_token;
settings.google_access_token_expiration = accessTokenExpiration;

//set google refresh token if it is returned
if(refresh_token != undefined) settings.google_refresh_token = refresh_token;

database.collection(mongoCollectionName).save(settings);

response.writeHead(200, {"Content-Type": "application/javascript"});
response.write('refresh token: ' + refresh_token + '\n');
response.write(JSON.stringify(res, null, '\t'));
response.end();
});
});

}

});
Expand Down

0 comments on commit f40d895

Please sign in to comment.