Skip to content

Commit c348b3c

Browse files
committed
nits: simplify code
* Some code paths use _login to create an anonymous user but this is overly complicated when they can just directly call createNewUser. * createNewUser will set the _user instance so there is no need to set it after the method returns * Now, _login is only used by the public login method, so remove this helper _login method.
1 parent 468b955 commit c348b3c

File tree

1 file changed

+12
-26
lines changed

1 file changed

+12
-26
lines changed

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OneSignalUserManagerImpl.swift

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,7 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
137137
}
138138

139139
// There is no user instance, initialize a "guest user"
140-
let user = _login(externalId: nil, token: nil)
141-
_user = user
142-
return user
140+
return createNewUser(externalId: nil, token: nil)
143141
}
144142

145143
var _user: OSUserInternal?
@@ -251,8 +249,7 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
251249
// Path 3. Creates an anonymous user if there isn't one in the cache nor a legacy player
252250
if _user == nil {
253251
// There is no user instance, initialize a "guest user"
254-
let user = createNewUser(externalId: nil, token: nil)
255-
_user = user
252+
_ = createNewUser(externalId: nil, token: nil)
256253
}
257254
}
258255

@@ -287,7 +284,16 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
287284
return
288285
}
289286
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignal.User login called with externalId: \(externalId)")
290-
_ = _login(externalId: externalId, token: token)
287+
288+
// Logging into an identified user from an anonymous user
289+
if let user = _user, user.isAnonymous {
290+
user.identityModel.jwtBearerToken = token
291+
identifyUser(externalId: externalId, currentUser: user)
292+
} else {
293+
// Logging into identified -> anon, identified -> identified, or nil -> identified
294+
_ = createNewUser(externalId: externalId, token: token)
295+
}
296+
291297
}
292298

293299
/**
@@ -397,26 +403,6 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
397403
OneSignalUserManagerImpl.sharedInstance.subscriptionModelStore.clearModelsFromStore()
398404
}
399405

400-
private func _login(externalId: String?, token: String?) -> OSUserInternal {
401-
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: nil) else {
402-
return _mockUser
403-
}
404-
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignalUserManager internal _login called with externalId: \(externalId ?? "nil")")
405-
406-
// If have token, validate token. Account for this being a requirement.
407-
// Logging into an identified user from an anonymous user
408-
if let externalId = externalId,
409-
let user = _user,
410-
user.isAnonymous {
411-
user.identityModel.jwtBearerToken = token
412-
identifyUser(externalId: externalId, currentUser: user)
413-
return self.user
414-
}
415-
416-
// Logging into anon -> anon, identified -> anon, identified -> identified, or nil -> any user
417-
return createNewUser(externalId: externalId, token: token)
418-
}
419-
420406
/**
421407
The SDK needs to have a user at all times, so this method will create a new anonymous user. If the current user is already anonymous, calling `logout` results in a no-op.
422408
*/

0 commit comments

Comments
 (0)