Skip to content

Commit 82c1d89

Browse files
committed
fix(oauth): Update Redis hash mapping when OAuth email changes
- Track old OAuth email to properly clean up Redis hash entries - Remove stale email mappings when Gmail updates user email - Ignore underscore-prefixed fields in account serialization - Fixes PubSub notification matching for accounts with updated emails This resolves issues where Google PubSub notifications fail to match accounts after email addresses are updated or corrected during OAuth profile synchronization.
1 parent 40cb42d commit 82c1d89

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/account.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ class Account {
194194
const counters = {};
195195

196196
Object.keys(accountData).forEach(key => {
197+
if (key.startsWith('_')) {
198+
return; //ignore
199+
}
200+
197201
let countMatch = key.match(/^stats:count:([^:]+):([^:]+)/);
198202
if (countMatch) {
199203
const [, type, counter] = countMatch;
@@ -387,6 +391,10 @@ class Account {
387391
let result = {};
388392

389393
Object.keys(accountData).forEach(key => {
394+
if (key.startsWith('_')) {
395+
// ignore
396+
return;
397+
}
390398
switch (key) {
391399
case 'notifyFrom':
392400
case 'syncFrom':
@@ -680,6 +688,9 @@ class Account {
680688

681689
if (addProvider && !LEGACY_KEYS.includes(addProvider)) {
682690
pipeline = pipeline.sadd(`${REDIS_PREFIX}oapp:a:${addProvider}`, this.account);
691+
if (accountData._oldOAuth2User && accountData._oldOAuth2User.toLowerCase() !== accountData.oauth2.auth?.user?.toLowerCase()) {
692+
pipeline = pipeline.hdel(`${REDIS_PREFIX}oapp:h:${addProvider}`, accountData._oldOAuth2User.toLowerCase());
693+
}
683694
if (accountData.oauth2?.auth?.user) {
684695
// check if already exists
685696
let existingAppBinding = await this.redis.hget(

lib/email-client/gmail-client.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ class GmailClient extends BaseClient {
316316

317317
// Update account email if changed
318318
if (profileRes.emailAddress && accountData.oauth2.auth?.user !== profileRes.emailAddress) {
319+
updates._oldOAuth2User = accountData.oauth2.auth?.user; // needed for cleanups
319320
updates.oauth2 = {
320321
partial: true,
321322
auth: Object.assign(accountData.oauth2.auth || {}, {

0 commit comments

Comments
 (0)