@@ -133,83 +133,90 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
133
133
func setEmail( _ email: String ? , authToken: String ? = nil , successHandler: OnSuccessHandler ? = nil , failureHandler: OnFailureHandler ? = nil , identityResolution: IterableIdentityResolution ? = nil ) {
134
134
135
135
ITBInfo ( )
136
-
137
- let merge = identityResolution? . mergeOnAnonymousToKnown ?? config. identityResolution. mergeOnAnonymousToKnown
138
- let replay = identityResolution? . replayOnVisitorToKnown ?? config. identityResolution. replayOnVisitorToKnown
139
-
140
136
if self . _email == email && email != nil {
141
137
self . checkAndUpdateAuthToken ( authToken)
142
138
return
143
139
}
144
-
140
+
145
141
if self . _email == email {
146
142
return
147
143
}
148
144
149
145
self . logoutPreviousUser ( )
150
-
146
+
151
147
self . _email = email
152
148
self . _userId = nil
153
-
154
- if config. enableAnonTracking, let email = email {
155
- attemptAndProcessMerge (
156
- merge: merge ?? true ,
157
- replay: replay ?? true ,
158
- destinationUser: email,
159
- isEmail: true ,
160
- failureHandler: failureHandler
161
- )
162
- self . localStorage. userIdAnnon = nil
149
+
150
+ self . onLogin ( authToken) { [ weak self] in
151
+ guard let config = self ? . config else {
152
+ return
153
+ }
154
+ let merge = identityResolution? . mergeOnAnonymousToKnown ?? config. identityResolution. mergeOnAnonymousToKnown
155
+ let replay = identityResolution? . replayOnVisitorToKnown ?? config. identityResolution. replayOnVisitorToKnown
156
+ if config. enableAnonTracking, let email = email {
157
+ self ? . attemptAndProcessMerge (
158
+ merge: merge ?? true ,
159
+ replay: replay ?? true ,
160
+ destinationUser: email,
161
+ isEmail: true ,
162
+ failureHandler: failureHandler
163
+ )
164
+ self ? . localStorage. userIdAnnon = nil
165
+ }
163
166
}
167
+
168
+
164
169
self . _successCallback = successHandler
165
170
self . _failureCallback = failureHandler
166
171
self . storeIdentifierData ( )
167
- self . onLogin ( authToken)
168
-
169
172
}
170
173
171
174
func setUserId( _ userId: String ? , authToken: String ? = nil , successHandler: OnSuccessHandler ? = nil , failureHandler: OnFailureHandler ? = nil , isAnon: Bool = false , identityResolution: IterableIdentityResolution ? = nil ) {
172
175
ITBInfo ( )
173
-
174
- let merge = identityResolution? . mergeOnAnonymousToKnown ?? config. identityResolution. mergeOnAnonymousToKnown
175
- let replay = identityResolution? . replayOnVisitorToKnown ?? config. identityResolution. replayOnVisitorToKnown
176
-
176
+
177
177
if self . _userId == userId && userId != nil {
178
178
self . checkAndUpdateAuthToken ( authToken)
179
179
return
180
180
}
181
-
181
+
182
182
if self . _userId == userId {
183
183
return
184
184
}
185
-
185
+
186
186
self . logoutPreviousUser ( )
187
-
187
+
188
188
self . _email = nil
189
189
self . _userId = userId
190
190
191
- if config. enableAnonTracking {
192
- if let userId = userId, userId != localStorage. userIdAnnon {
193
- attemptAndProcessMerge (
194
- merge: merge ?? true ,
195
- replay: replay ?? true ,
196
- destinationUser: userId,
197
- isEmail: false ,
198
- failureHandler: failureHandler
199
- )
191
+ self . onLogin ( authToken) { [ weak self] in
192
+ guard let config = self ? . config else {
193
+ return
200
194
}
201
-
202
- if !isAnon {
203
- localStorage. userIdAnnon = nil
195
+ if config. enableAnonTracking {
196
+ if let userId = userId, userId != ( self ? . localStorage. userIdAnnon ?? " " ) {
197
+ let merge = identityResolution? . mergeOnAnonymousToKnown ?? config. identityResolution. mergeOnAnonymousToKnown
198
+ let replay = identityResolution? . replayOnVisitorToKnown ?? config. identityResolution. replayOnVisitorToKnown
199
+ self ? . attemptAndProcessMerge (
200
+ merge: merge ?? true ,
201
+ replay: replay ?? true ,
202
+ destinationUser: userId,
203
+ isEmail: false ,
204
+ failureHandler: failureHandler
205
+ )
206
+ }
207
+
208
+ if !isAnon {
209
+ self ? . localStorage. userIdAnnon = nil
210
+ }
204
211
}
205
212
}
206
-
213
+
207
214
self . _successCallback = successHandler
208
215
self . _failureCallback = failureHandler
209
216
self . storeIdentifierData ( )
210
- self . onLogin ( authToken )
217
+
211
218
}
212
-
219
+
213
220
func logoutUser( ) {
214
221
logoutPreviousUser ( )
215
222
}
@@ -233,6 +240,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
233
240
self . localStorage. anonymousUserEvents = nil
234
241
self . localStorage. anonymousSessions = nil
235
242
self . localStorage. anonymousUserUpdate = nil
243
+ self . localStorage. userIdAnnon = nil
236
244
237
245
if isAnonymousUsageTracked && config. enableAnonTracking {
238
246
ITBInfo ( " CONSENT GIVEN and ANON TRACKING ENABLED - Criteria fetched " )
@@ -738,35 +746,34 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
738
746
localStorage. userId = _userId
739
747
}
740
748
741
- private func onLogin( _ authToken: String ? = nil ) {
749
+ private func onLogin( _ authToken: String ? = nil , onloginSuccess onloginSuccessCallBack : ( ( ) -> ( ) ) ? = nil ) {
742
750
guard isSDKInitialized ( ) else { return }
743
751
744
752
ITBInfo ( )
745
753
746
754
self . authManager. pauseAuthRetries ( false )
747
755
if let authToken {
748
756
self . authManager. setNewToken ( authToken)
749
- completeUserLogin ( )
757
+ completeUserLogin ( onloginSuccessCallBack : onloginSuccessCallBack )
750
758
} else if isEitherUserIdOrEmailSet ( ) && config. authDelegate != nil {
751
- requestNewAuthToken ( )
759
+ requestNewAuthToken ( onloginSuccessCallBack : onloginSuccessCallBack )
752
760
} else {
753
- completeUserLogin ( )
761
+ completeUserLogin ( onloginSuccessCallBack : onloginSuccessCallBack )
754
762
}
755
763
}
756
764
757
- private func requestNewAuthToken( ) {
765
+ private func requestNewAuthToken( onloginSuccessCallBack : ( ( ) -> ( ) ) ? = nil ) {
758
766
ITBInfo ( )
759
767
760
768
authManager. requestNewAuthToken ( hasFailedPriorAuth: false , onSuccess: { [ weak self] token in
761
769
if token != nil {
762
- self ? . completeUserLogin ( )
770
+ self ? . completeUserLogin ( onloginSuccessCallBack : onloginSuccessCallBack )
763
771
}
764
772
} , shouldIgnoreRetryPolicy: true )
765
773
}
766
774
767
- private func completeUserLogin( ) {
768
- ITBInfo ( )
769
-
775
+ private func completeUserLogin( onloginSuccessCallBack: ( ( ) -> ( ) ) ? = nil ) {
776
+ ITBInfo ( )
770
777
guard isSDKInitialized ( ) else { return }
771
778
772
779
if config. autoPushRegistration {
@@ -776,6 +783,9 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
776
783
}
777
784
778
785
_ = inAppManager. scheduleSync ( )
786
+ if onloginSuccessCallBack != nil {
787
+ onloginSuccessCallBack!( )
788
+ }
779
789
}
780
790
781
791
private func retrieveIdentifierData( ) {
0 commit comments