Skip to content

Commit dcb2cab

Browse files
committed
For user login, send correct data based on identified status of prev user
Motivation: we want to make sure to send the correct data based on whether the previous user was identified or not. If we are identifying an anonymous user, we should send the entire user data. If we are upserting a user, we should only send the push subscription and external id.
1 parent 5657714 commit dcb2cab

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/onesignal/OneSignal.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import { OneSignalDeferredLoadedCallback } from "../page/models/OneSignalDeferre
4545
import UserDirector from "./UserDirector";
4646
import { ModelName, SupportedModel } from "../core/models/SupportedModels";
4747
import { OSModel } from "../core/modelRepo/OSModel";
48+
import UserData from "../core/models/UserData";
4849

4950
export default class OneSignal {
5051
private static async _initializeCoreModuleAndUserNamespace() {
@@ -128,7 +129,21 @@ export default class OneSignal {
128129
// set the external id on the user locally
129130
LoginManager.setExternalId(identityModel, externalId);
130131

131-
const userData = await UserDirector.getAllUserData();
132+
let userData: Partial<UserData>;
133+
const pushSubscription = await this.coreDirector.getCurrentPushSubscriptionModel();
134+
if (!isIdentified) {
135+
userData = await UserDirector.getAllUserData();
136+
} else {
137+
userData = {
138+
identity: {
139+
external_id: externalId,
140+
}
141+
};
142+
143+
if (pushSubscription) {
144+
userData.subscriptions = [pushSubscription.data];
145+
}
146+
}
132147
await this.coreDirector.resetModelRepoAndCache();
133148
await UserDirector.initializeUser(true);
134149
await OneSignal.User.PushSubscription._resubscribeToPushModelChanges();

src/page/managers/LoginManager.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default class LoginManager {
2626
return identity.external_id !== undefined;
2727
}
2828

29-
static async upsertUser(userData: UserData): Promise<UserData> {
29+
static async upsertUser(userData: Partial<UserData>): Promise<UserData> {
3030
logMethodCall("LoginManager.upsertUser", { userData });
3131
const appId = await MainHelper.getAppId();
3232
this.prepareIdentityForUpsert(userData);
@@ -89,18 +89,19 @@ export default class LoginManager {
8989
return { identity: identityResult };
9090
}
9191

92-
static async identifyOrUpsertUser(userData: UserData, isIdentified: boolean, subscriptionId?: string)
92+
static async identifyOrUpsertUser(userData: Partial<UserData>, isIdentified: boolean, subscriptionId?: string)
9393
: Promise<Partial<UserData>> {
9494
logMethodCall("LoginManager.identifyOrUpsertUser", { userData, isIdentified, subscriptionId });
9595

9696
let result: Partial<UserData>;
9797

9898
if (isIdentified) {
99-
// if started off identified, create a new user
99+
// if started off identified, upsert a user
100100
result = await this.upsertUser(userData);
101101
} else {
102102
// promoting anonymous user to identified user
103-
result = await this.identifyUser(userData, subscriptionId);
103+
// from user data, we only use identity (and we remove all aliases except external_id)
104+
result = await this.identifyUser(userData as UserData, subscriptionId);
104105
}
105106
return result;
106107
}
@@ -121,7 +122,7 @@ export default class LoginManager {
121122
* if logging in from identified user a to identified user b, the identity object would
122123
* otherwise contain any existing user a aliases
123124
*/
124-
static prepareIdentityForUpsert(userData: UserData): void {
125+
static prepareIdentityForUpsert(userData: Partial<UserData>): void {
125126
logMethodCall("LoginManager.prepareIdentityForUpsert", { userData });
126127

127128
const { identity } = userData;

0 commit comments

Comments
 (0)