@@ -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] ( ) -> Void in
151
+ guard let self = self 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
+ 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] ( ) -> Void in
192
+ guard let self = self else {
193
+ return
200
194
}
201
-
202
- if !isAnon {
203
- localStorage. userIdAnnon = nil
195
+ if config. enableAnonTracking {
196
+ if let userId = userId, userId != localStorage. userIdAnnon {
197
+ let merge = identityResolution? . mergeOnAnonymousToKnown ?? config. identityResolution. mergeOnAnonymousToKnown
198
+ let replay = identityResolution? . replayOnVisitorToKnown ?? config. identityResolution. replayOnVisitorToKnown
199
+ 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
+ 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 " )
@@ -726,35 +734,35 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
726
734
private func storeIdentifierData( ) {
727
735
localStorage. email = _email
728
736
localStorage. userId = _userId
737
+
729
738
}
730
739
731
- private func onLogin( _ authToken: String ? = nil ) {
740
+ private func onLogin( _ authToken: String ? = nil , onloginSuccess onloginSuccessCallBack : ( ( ) -> ( ) ) ? = nil ) {
732
741
ITBInfo ( )
733
742
734
743
self . authManager. pauseAuthRetries ( false )
735
744
if let authToken = authToken {
736
745
self . authManager. setNewToken ( authToken)
737
- completeUserLogin ( )
746
+ completeUserLogin ( onloginSuccessCallBack : onloginSuccessCallBack )
738
747
} else if isEitherUserIdOrEmailSet ( ) && config. authDelegate != nil {
739
- requestNewAuthToken ( )
748
+ requestNewAuthToken ( onloginSuccessCallBack : onloginSuccessCallBack )
740
749
} else {
741
- completeUserLogin ( )
750
+ completeUserLogin ( onloginSuccessCallBack : onloginSuccessCallBack )
742
751
}
743
752
}
744
753
745
- private func requestNewAuthToken( ) {
754
+ private func requestNewAuthToken( onloginSuccessCallBack : ( ( ) -> ( ) ) ? = nil ) {
746
755
ITBInfo ( )
747
756
748
757
authManager. requestNewAuthToken ( hasFailedPriorAuth: false , onSuccess: { [ weak self] token in
749
758
if token != nil {
750
- self ? . completeUserLogin ( )
759
+ self ? . completeUserLogin ( onloginSuccessCallBack : onloginSuccessCallBack )
751
760
}
752
761
} , shouldIgnoreRetryPolicy: true )
753
762
}
754
763
755
- private func completeUserLogin( ) {
764
+ private func completeUserLogin( onloginSuccessCallBack : ( ( ) -> ( ) ) ? = nil ) {
756
765
ITBInfo ( )
757
-
758
766
guard isEitherUserIdOrEmailSet ( ) else {
759
767
return
760
768
}
@@ -766,6 +774,9 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
766
774
}
767
775
768
776
_ = inAppManager. scheduleSync ( )
777
+ if onloginSuccessCallBack != nil {
778
+ onloginSuccessCallBack!( )
779
+ }
769
780
}
770
781
771
782
private func retrieveIdentifierData( ) {
0 commit comments