mirror of
https://github.com/moparisthebest/mail
synced 2025-01-08 12:08:07 -05:00
add emailaddress caching at login
This commit is contained in:
parent
7fe8755b99
commit
c4b4999814
@ -76,21 +76,54 @@ define(function(require) {
|
||||
* Lookup the user's email address. Check local cache if available, otherwise query google's token info api to learn the user's email address
|
||||
*/
|
||||
self.queryEmailAddress = function(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) {
|
||||
callback(null, info.email);
|
||||
},
|
||||
error: function(xhr, textStatus, err) {
|
||||
callback({
|
||||
errMsg: xhr.status + ': ' + xhr.statusText,
|
||||
err: err
|
||||
});
|
||||
}
|
||||
var deviceStorage, key = 'emailaddress';
|
||||
|
||||
// check device storage
|
||||
deviceStorage = new DeviceStorageDAO();
|
||||
deviceStorage.init('app-config', function() {
|
||||
deviceStorage.listItems(key, 0, null, function(err, cachedItems) {
|
||||
if (err) {
|
||||
callback(err);
|
||||
return;
|
||||
}
|
||||
|
||||
// do roundtrip to google api if no email address is cached yet
|
||||
if (!cachedItems || cachedItems.length < 1) {
|
||||
queryGoogleApi();
|
||||
return;
|
||||
}
|
||||
|
||||
callback(null, cachedItems[0]);
|
||||
});
|
||||
});
|
||||
|
||||
function queryGoogleApi() {
|
||||
// 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) {
|
||||
if (!info || !info.email) {
|
||||
callback({
|
||||
errMsg: 'Error looking up email address on google api!'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// cache the email address on the device
|
||||
deviceStorage.storeList([info.email], key, function(err) {
|
||||
callback(err, info.email);
|
||||
});
|
||||
},
|
||||
error: function(xhr, textStatus, err) {
|
||||
callback({
|
||||
errMsg: xhr.status + ': ' + xhr.statusText,
|
||||
err: err
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -3,6 +3,7 @@ define(function(require) {
|
||||
|
||||
var controller = require('js/app-controller'),
|
||||
EmailDAO = require('js/dao/email-dao'),
|
||||
DeviceStorageDAO = require('js/dao/devicestorage-dao'),
|
||||
$ = require('jquery'),
|
||||
expect = chai.expect;
|
||||
|
||||
@ -50,12 +51,30 @@ define(function(require) {
|
||||
});
|
||||
|
||||
describe('login', function() {
|
||||
it('should work', function(done) {
|
||||
it('should work the first time', function(done) {
|
||||
// clear db
|
||||
var deviceStorage = new DeviceStorageDAO();
|
||||
deviceStorage.init('app-config', function() {
|
||||
deviceStorage.clear(function() {
|
||||
|
||||
// do test with fresh db
|
||||
controller.fetchOAuthToken(appControllerTest.passphrase, function(err, userId) {
|
||||
expect(err).to.not.exist;
|
||||
expect(userId).to.equal(appControllerTest.user);
|
||||
expect(window.chrome.identity.getAuthToken.calledOnce).to.be.true;
|
||||
expect($.ajax.calledOnce).to.be.true;
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should work when the email address is cached', function(done) {
|
||||
controller.fetchOAuthToken(appControllerTest.passphrase, function(err, userId) {
|
||||
expect(err).to.not.exist;
|
||||
expect(userId).to.equal(appControllerTest.user);
|
||||
expect($.ajax.calledOnce).to.be.true;
|
||||
expect(window.chrome.identity.getAuthToken.calledOnce).to.be.true;
|
||||
expect($.ajax.called).to.be.false;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user