Skip to content

Commit ae4d2ac

Browse files
authored
fix: only createUserOnServer on logout if there are any subscriptions (#1422)
1 parent b833c26 commit ae4d2ac

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
},
8686
{
8787
"path": "./build/releases/OneSignalSDK.page.es6.js",
88-
"limit": "46.23 kB",
88+
"limit": "46.24 kB",
8989
"gzip": true
9090
},
9191
{

src/page/managers/LoginManager.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,20 @@ describe('LoginManager', () => {
6363
);
6464
});
6565

66-
test('logout: with external id resets models and creates anonymous user', async () => {
66+
test('logout: with external id and no subscriptions resets models and skips user creation', async () => {
6767
await updateIdentityModel('external_id', 'abc');
68+
await LoginManager.logout();
69+
expect(createUserOnServerSpy).not.toHaveBeenCalled();
70+
});
71+
72+
test('logout: with external id and a subscription resets models and creates anonymous user', async () => {
73+
await updateIdentityModel('external_id', 'abc');
74+
const mockSub = { id: 'sub-id' } as SubscriptionModel;
75+
vi.spyOn(
76+
OneSignal._coreDirector._subscriptionModelStore,
77+
'_list',
78+
).mockReturnValue([mockSub]);
79+
6880
await LoginManager.logout();
6981
expect(createUserOnServerSpy).toHaveBeenCalled();
7082
});

src/page/managers/LoginManager.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,15 @@ export default class LoginManager {
9393
if (!identityModel._externalId)
9494
return Log._debug('Logout: User is not logged in, skipping logout');
9595

96+
const hasAnySubscription =
97+
OneSignal._coreDirector._subscriptionModelStore._list().length > 0;
98+
9699
resetUserModels();
97100

98-
// create a new anonymous user
99-
return createUserOnServer();
101+
// Only create an anonymous user if there is a subscription to associate with it.
102+
// Without a subscription, there is nothing meaningful to register on the server.
103+
if (hasAnySubscription) {
104+
return createUserOnServer();
105+
}
100106
}
101107
}

0 commit comments

Comments
 (0)