Skip to content

Commit 9a26368

Browse files
authored
Merge pull request #850 from Iterable/joao/MOB-9946-ios-add-auth-checks-for-api-call-methods
MOB-9946 Add auth checks for api call methods
2 parents b137b86 + 3f449ae commit 9a26368

File tree

2 files changed

+167
-61
lines changed

2 files changed

+167
-61
lines changed

swift-sdk/Internal/InternalIterableAPI.swift

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
250250
func register(token: Data,
251251
onSuccess: OnSuccessHandler? = nil,
252252
onFailure: OnFailureHandler? = nil) {
253+
253254
guard let appName = pushIntegrationName else {
254255
let errorMessage = "Not registering device token - appName must not be nil"
255256
ITBError(errorMessage)
@@ -566,7 +567,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
566567
source: InAppDeleteSource? = nil,
567568
inboxSessionId: String? = nil,
568569
onSuccess: OnSuccessHandler? = nil,
569-
onFailure: OnFailureHandler? = nil) -> Pending<SendRequestValue, SendRequestError> {
570+
onFailure: OnFailureHandler? = nil) -> Pending<SendRequestValue, SendRequestError> {
570571
requestHandler.inAppConsume(message: message,
571572
location: location,
572573
source: source,
@@ -691,6 +692,17 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
691692
}
692693
}
693694

695+
696+
func isSDKInitialized() -> Bool {
697+
let isInitialized = !apiKey.isEmpty && isEitherUserIdOrEmailSet()
698+
699+
if !isInitialized {
700+
ITBInfo("Iterable SDK must be initialized with an API key and user email/userId before calling SDK methods")
701+
}
702+
703+
return isInitialized
704+
}
705+
694706
public func isEitherUserIdOrEmailSet() -> Bool {
695707
IterableUtil.isNotNullOrEmpty(string: _email) || IterableUtil.isNotNullOrEmpty(string: _userId)
696708
}
@@ -702,9 +714,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
702714
private func logoutPreviousUser() {
703715
ITBInfo()
704716

705-
guard isEitherUserIdOrEmailSet() else {
706-
return
707-
}
717+
guard isSDKInitialized() else { return }
708718

709719
if config.autoPushRegistration {
710720
disableDeviceForCurrentUser()
@@ -729,10 +739,12 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
729739
}
730740

731741
private func onLogin(_ authToken: String? = nil) {
742+
guard isSDKInitialized() else { return }
743+
732744
ITBInfo()
733745

734746
self.authManager.pauseAuthRetries(false)
735-
if let authToken = authToken {
747+
if let authToken {
736748
self.authManager.setNewToken(authToken)
737749
completeUserLogin()
738750
} else if isEitherUserIdOrEmailSet() && config.authDelegate != nil {
@@ -755,9 +767,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
755767
private func completeUserLogin() {
756768
ITBInfo()
757769

758-
guard isEitherUserIdOrEmailSet() else {
759-
return
760-
}
770+
guard isSDKInitialized() else { return }
761771

762772
if config.autoPushRegistration {
763773
notificationStateProvider.registerForRemoteNotifications()

0 commit comments

Comments
 (0)