diff --git a/packages/nylas-api/routes/auth.js b/packages/nylas-api/routes/auth.js index d0cb6949e..5cd1e2fb4 100644 --- a/packages/nylas-api/routes/auth.js +++ b/packages/nylas-api/routes/auth.js @@ -43,24 +43,33 @@ const buildAccountWith = ({name, email, provider, settings, credentials}) => { return DatabaseConnector.forShared().then((db) => { const {AccountToken, Account} = db; - const account = Account.build({ - name: name, - provider: provider, - emailAddress: email, - connectionSettings: settings, - syncPolicy: SyncPolicy.defaultPolicy(), - lastSyncCompletions: [], - }) - account.setCredentials(credentials); + return Account.find({ + where: { + emailAddress: email, + connectionSettings: JSON.stringify(settings), + }, + }).then((existing) => { + const account = existing || Account.build({ + name: name, + provider: provider, + emailAddress: email, + connectionSettings: settings, + syncPolicy: SyncPolicy.defaultPolicy(), + lastSyncCompletions: [], + }) - return account.save().then((saved) => - AccountToken.create({accountId: saved.id}).then((token) => - DatabaseConnector.prepareAccountDatabase(saved.id).thenReturn({ - account: saved, - token: token, - }) - ) - ); + // always update with the latest credentials + account.setCredentials(credentials); + + return account.save().then((saved) => + AccountToken.create({accountId: saved.id}).then((token) => + DatabaseConnector.prepareAccountDatabase(saved.id).thenReturn({ + account: saved, + token: token, + }) + ) + ); + }); }); } @@ -76,7 +85,6 @@ module.exports = (server) => { validate: { query: { client_id: Joi.string().required(), - n1_id: Joi.string(), }, payload: { email: Joi.string().email().required(), @@ -98,7 +106,11 @@ module.exports = (server) => { const {settings, email, provider, name} = request.payload; if (provider === 'imap') { - connectionChecks.push(IMAPConnection.connect({db: dbStub, settings})) + connectionChecks.push(IMAPConnection.connect({ + logger: request.logger, + settings: settings, + db: dbStub, + })); } Promise.all(connectionChecks).then(() => { @@ -187,9 +199,12 @@ module.exports = (server) => { client_id: GMAIL_CLIENT_ID, client_secret: GMAIL_CLIENT_SECRET, } - Promise.all([ - IMAPConnection.connect({db: {}, settings: Object.assign({}, settings, credentials)}), + IMAPConnection.connect({ + logger: request.logger, + settings: Object.assign({}, settings, credentials), + db: {}, + }), ]) .then(() => buildAccountWith({ diff --git a/packages/nylas-core/imap-connection.js b/packages/nylas-core/imap-connection.js index 05e703b49..d833a7936 100644 --- a/packages/nylas-core/imap-connection.js +++ b/packages/nylas-core/imap-connection.js @@ -156,8 +156,6 @@ class IMAPBox { } return this._imap.closeBoxAsync(expunge) } - - } @@ -178,6 +176,11 @@ class IMAPConnection extends EventEmitter { constructor({db, settings, logger = console} = {}) { super(); + + if (!(settings instanceof Object)) { + throw new Error("IMAPConnection: Must be instantiated with `settings`") + } + this._logger = logger; this._db = db; this._queue = []; diff --git a/packages/nylas-dashboard/public/js/app.jsx b/packages/nylas-dashboard/public/js/app.jsx index bed514410..ecf7a1f7d 100644 --- a/packages/nylas-dashboard/public/js/app.jsx +++ b/packages/nylas-dashboard/public/js/app.jsx @@ -1,4 +1,6 @@ /* eslint react/react-in-jsx-scope: 0*/ +/* eslint no-console: 0*/ + const React = window.React; const ReactDOM = window.ReactDOM; const { diff --git a/packages/nylas-sync/sync-worker.js b/packages/nylas-sync/sync-worker.js index 1dfb377b9..7178e59cf 100644 --- a/packages/nylas-sync/sync-worker.js +++ b/packages/nylas-sync/sync-worker.js @@ -108,7 +108,12 @@ class SyncWorker { return Promise.reject(new Error("ensureConnection: There are no IMAP connection credentials for this account.")) } - const conn = new IMAPConnection({db: this._db, settings: Object.assign({}, settings, credentials), logger: this._logger}); + const conn = new IMAPConnection({ + db: this._db, + settings: Object.assign({}, settings, credentials), + logger: this._logger, + }); + conn.on('mail', () => { this._onConnectionIdleUpdate(); })