@@ -380,17 +380,29 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
380380 OneSignalUserManagerImpl . sharedInstance. subscriptionModelStore. clearModelsFromStore ( )
381381 }
382382
383+ /**
384+ Entry point to creating and setting a user. It is called by the SDK to generate an anonymous user and also
385+ by clients to login a user.
386+ */
383387 private func _login( externalId: String ? , token: String ? ) -> OSUserInternal {
384388 guard !OneSignalConfigManager. shouldAwaitAppIdAndLogMissingPrivacyConsent ( forMethod: nil ) else {
385389 return _mockUser
386390 }
387391 OneSignalLog . onesignalLog ( . LL_VERBOSE, message: " OneSignalUserManager internal _login called with externalId: \( externalId ?? " nil " ) " )
388392
389- // Logging into an identified user from an anonymous user, if JWT is not ON
393+ /*
394+ Logging in to a "new-to-the-sdk" externalId from an anonymous user, if JWT is OFF or UNKNOWN.
395+
396+ Note: If we are logging in to an externalId that already exists in the SDK, from an anon user, we know the client has called:
397+ login(userA) -> logout -> login(userA)
398+ The userA is expected to exist and will not result in successfully identifying the anonymous user;
399+ this login flow will instead fall into the createUser path below.
400+ */
390401 if let externalId = externalId,
391402 let user = _user,
392403 user. isAnonymous,
393- jwtConfig. isRequired != true
404+ jwtConfig. isRequired != true ,
405+ identityModelRepo. get ( externalId: externalId) == nil
394406 {
395407 user. identityModel. jwtBearerToken = token
396408 identifyUser ( externalId: externalId, currentUser: user)
@@ -449,15 +461,25 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
449461 */
450462 func setNewInternalUser( externalId: String ? , pushSubscriptionModel: OSSubscriptionModel ? ) -> OSUserInternal {
451463 let aliases : [ String : String ] ?
464+ let identityModel : OSIdentityModel
465+
452466 if let externalIdToUse = externalId {
453467 aliases = [ OS_EXTERNAL_ID: externalIdToUse]
454468 } else {
455469 aliases = nil
456470 }
457471
458- let identityModel = OSIdentityModel ( aliases: aliases, changeNotifier: OSEventProducer ( ) )
472+ // If there is an existing identity model with the same external ID, use it
473+ if let externalId = externalId,
474+ let existingIdentityModel = identityModelRepo. get ( externalId: externalId)
475+ {
476+ identityModel = existingIdentityModel
477+ } else {
478+ identityModel = OSIdentityModel ( aliases: aliases, changeNotifier: OSEventProducer ( ) )
479+ self . addIdentityModelToRepo ( identityModel)
480+ }
481+
459482 self . identityModelStore. add ( id: OS_IDENTITY_MODEL_KEY, model: identityModel, hydrating: false )
460- self . addIdentityModelToRepo ( identityModel)
461483
462484 let propertiesModel = OSPropertiesModel ( changeNotifier: OSEventProducer ( ) )
463485 self . propertiesModelStore. add ( id: OS_PROPERTIES_MODEL_KEY, model: propertiesModel, hydrating: false )
0 commit comments