Skip to content

Commit

Permalink
Fix undefined subscription id
Browse files Browse the repository at this point in the history
Fixes the OneSignal-Subscription-Id header being undefined on login from an identified user to another identified user. The pushSubscription model at this point in time is undefined and has no id. Now the subscription id will be passed up from login in the same fashion as identify user.
  • Loading branch information
shepherd-l committed Mar 5, 2024
1 parent bf44683 commit 48777f2
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions src/page/managers/LoginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export default class LoginManager {

if (isIdentified) {
// if started off identified, upsert a user
result = await this.upsertUser(userData);
result = await this.upsertUser(userData, subscriptionId);
} else {
// promoting anonymous user to identified user
// from user data, we only use identity (and we remove all aliases except external_id)
Expand All @@ -196,23 +196,16 @@ export default class LoginManager {

static async upsertUser(
userData: Partial<UserData>,
subscriptionId?: string,
retry = 5,
): Promise<UserData> {
logMethodCall('LoginManager.upsertUser', { userData });
logMethodCall('LoginManager.upsertUser', { userData, subscriptionId });

if (retry === 0) {
throw new OneSignalError('Login: upsertUser failed: max retries reached');
}

const appId = await MainHelper.getAppId();
const pushSubscription =
await OneSignal.coreDirector.getPushSubscriptionModel();

let subscriptionId;
if (isCompleteSubscriptionObject(pushSubscription?.data)) {
subscriptionId = pushSubscription?.data.id;
}

const userDataCopy = JSON.parse(JSON.stringify(userData));

// only accepts one alias, so remove other aliases only leaving external_id
Expand All @@ -232,7 +225,7 @@ export default class LoginManager {
} else if (status >= 500) {
Log.error('Server error. Retrying...');
await awaitableTimeout(RETRY_BACKOFF[retry]);
return this.upsertUser(userDataCopy, retry - 1);
return this.upsertUser(userDataCopy, subscriptionId, retry - 1);
}

return result;
Expand Down

0 comments on commit 48777f2

Please sign in to comment.