diff --git a/src/js/app-controller.js b/src/js/app-controller.js index fa448cf..6304c4c 100644 --- a/src/js/app-controller.js +++ b/src/js/app-controller.js @@ -39,9 +39,10 @@ define(['jquery', 'ImapClient', 'SmtpClient', 'js/dao/email-dao', 'js/dao/keycha self.execute = function(cmd, args, callback) { if (cmd === 'login') { // login user - fetchOAuthToken(args.userId, args.password, function(err) { + fetchOAuthToken(args.password, function(err, userId) { callback({ - err: err + err: err, + userId: userId }); }); @@ -92,13 +93,32 @@ define(['jquery', 'ImapClient', 'SmtpClient', 'js/dao/email-dao', 'js/dao/keycha // Helper methods // - function fetchOAuthToken(userId, password, callback) { + function fetchOAuthToken(password, callback) { // get OAuth Token from chrome chrome.identity.getAuthToken({ 'interactive': true }, function(token) { - login(userId, password, token, callback); + // fetch gmail user's email address from the Google Authorization Server endpoint + $.ajax({ + url: 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' + token, + type: 'GET', + dataType: 'json', + success: function(info) { + // login using the received email address + login(info.email, password, token, function(err) { + // send email address to sandbox + callback(err, info.email); + }); + }, + error: function(xhr, textStatus, err) { + callback({ + errMsg: xhr.status + ': ' + xhr.statusText, + err: err + }); + } + }); + } ); } diff --git a/src/js/view/login-view.js b/src/js/view/login-view.js index db88cec..866beda 100644 --- a/src/js/view/login-view.js +++ b/src/js/view/login-view.js @@ -46,7 +46,7 @@ define(['jquery', 'underscore', 'backbone', 'js/app-config'], function($, _, Bac return; } - window.location = '#accounts/' + userId + '/folders'; + window.location = '#accounts/' + resArgs.userId + '/folders'; }); } }); diff --git a/src/manifest.json b/src/manifest.json index a498e77..0480c0c 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -17,6 +17,7 @@ "oauth2": { "client_id": "440907777130.apps.googleusercontent.com", "scopes": [ + "https://www.googleapis.com/auth/userinfo.email", "https://mail.google.com/" ] }, diff --git a/src/tpl/login.html b/src/tpl/login.html index 9bfb014..7dcf359 100644 --- a/src/tpl/login.html +++ b/src/tpl/login.html @@ -7,7 +7,7 @@

Please sign in

- +