Skip to content

Commit 07173cd

Browse files
authored
Merge pull request #540 from Iterable/jay/MOB-4239-updateEmail-withToken
[MOB-4239] fix update email with token
2 parents 6bfea1a + d0e0390 commit 07173cd

File tree

5 files changed

+72
-15
lines changed

5 files changed

+72
-15
lines changed

swift-sdk/Internal/AuthManager.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ class AuthManager: IterableAuthManagerProtocol {
5353
}
5454
}
5555

56+
func setNewToken(_ newToken: String) {
57+
ITBInfo()
58+
59+
onAuthTokenReceived(retrievedAuthToken: newToken)
60+
}
61+
5662
func logoutUser() {
5763
ITBInfo()
5864

@@ -86,7 +92,7 @@ class AuthManager: IterableAuthManagerProtocol {
8692
queueAuthTokenExpirationRefresh(authToken)
8793
}
8894

89-
private func onAuthTokenReceived(retrievedAuthToken: String?, onSuccess: AuthTokenRetrievalHandler?) {
95+
private func onAuthTokenReceived(retrievedAuthToken: String?, onSuccess: AuthTokenRetrievalHandler? = nil) {
9096
pendingAuth = false
9197

9298
authToken = retrievedAuthToken

swift-sdk/Internal/InternalIterableAPI.swift

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
9595
deviceAttributes.removeValue(forKey: name)
9696
}
9797

98-
func setEmail(_ email: String?) {
98+
func setEmail(_ email: String?, authToken: String? = nil) {
9999
ITBInfo()
100100

101101
if _email == email {
@@ -109,10 +109,10 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
109109

110110
storeIdentifierData()
111111

112-
onLogin()
112+
onLogin(authToken)
113113
}
114114

115-
func setUserId(_ userId: String?) {
115+
func setUserId(_ userId: String?, authToken: String? = nil) {
116116
ITBInfo()
117117

118118
if _userId == userId {
@@ -126,7 +126,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
126126

127127
storeIdentifierData()
128128

129-
onLogin()
129+
onLogin(authToken)
130130
}
131131

132132
func logoutUser() {
@@ -200,10 +200,13 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
200200
withToken token: String? = nil,
201201
onSuccess: OnSuccessHandler? = nil,
202202
onFailure: OnFailureHandler? = nil) -> Pending<SendRequestValue, SendRequestError> {
203-
requestHandler.updateEmail(newEmail, onSuccess: nil, onFailure: nil).onSuccess { json in
203+
requestHandler.updateEmail(newEmail,
204+
onSuccess: nil,
205+
onFailure: nil).onSuccess { json in
204206
if self.email != nil {
205-
self.setEmail(newEmail)
207+
self.setEmail(newEmail, authToken: token)
206208
}
209+
207210
onSuccess?(json)
208211
}.onError { error in
209212
onFailure?(error.reason, error.data)
@@ -475,10 +478,13 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
475478
localStorage.userId = _userId
476479
}
477480

478-
private func onLogin() {
481+
private func onLogin(_ authToken: String? = nil) {
479482
ITBInfo()
480483

481-
if isEitherUserIdOrEmailSet() && config.authDelegate != nil {
484+
if let authToken = authToken {
485+
self.authManager.setNewToken(authToken)
486+
completeUserLogin()
487+
} else if isEitherUserIdOrEmailSet() && config.authDelegate != nil {
482488
requestNewAuthToken()
483489
} else {
484490
completeUserLogin()

swift-sdk/IterableAPI.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public final class IterableAPI: NSObject {
309309
///
310310
/// - Parameters:
311311
/// - newEmail: The new email of this user
312-
/// - token: The new authentication token for this user
312+
/// - token: The new authentication token for this user, if left out, the SDK will not update the token in any way
313313
/// - onSuccess: `OnSuccessHandler` to invoke if update is successful
314314
/// - onFailure: `OnFailureHandler` to invoke if update fails
315315
///
@@ -321,7 +321,7 @@ public final class IterableAPI: NSObject {
321321
withToken token: String,
322322
onSuccess: OnSuccessHandler?,
323323
onFailure: OnFailureHandler?) {
324-
internalImplementation?.updateEmail(newEmail, onSuccess: onSuccess, onFailure: onFailure)
324+
internalImplementation?.updateEmail(newEmail, withToken: token, onSuccess: onSuccess, onFailure: onFailure)
325325
}
326326

327327
/// Tracks what's in the shopping cart (or equivalent) at this point in time

swift-sdk/IterableAuthManagerProtocol.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ import Foundation
88
func getAuthToken() -> String?
99
func resetFailedAuthCount()
1010
func requestNewAuthToken(hasFailedPriorAuth: Bool, onSuccess: ((String?) -> Void)?)
11+
func setNewToken(_ newToken: String)
1112
func logoutUser()
1213
}

tests/unit-tests/AuthTests.swift

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class AuthTests: XCTestCase {
9090
XCTAssertNil(internalAPI.auth.authToken)
9191
}
9292

93-
func testNewEmailWithTokenChange() {
93+
func testNewEmailAndThenChangeToken() {
9494
var internalAPI: InternalIterableAPI?
9595

9696
let originalEmail = "[email protected]"
@@ -128,7 +128,7 @@ class AuthTests: XCTestCase {
128128
XCTAssertEqual(API.auth.authToken, newToken)
129129
}
130130

131-
func testNewUserIdWithTokenChange() {
131+
func testNewUserIdAndThenChangeToken() {
132132
var internalAPI: InternalIterableAPI?
133133

134134
let originalUserId = "firstUserId"
@@ -166,8 +166,8 @@ class AuthTests: XCTestCase {
166166
XCTAssertEqual(API.auth.authToken, newToken)
167167
}
168168

169-
func testUpdateEmailWithToken() {
170-
let condition1 = expectation(description: "update email with auth token")
169+
func testUpdateEmailAndThenChangeToken() {
170+
let condition1 = expectation(description: "update email and then change auth token")
171171

172172
var internalAPI: InternalIterableAPI?
173173

@@ -211,6 +211,50 @@ class AuthTests: XCTestCase {
211211
wait(for: [condition1], timeout: testExpectationTimeout)
212212
}
213213

214+
func testUpdateEmailWithTokenParam() {
215+
let condition1 = expectation(description: #function)
216+
217+
var internalAPI: InternalIterableAPI?
218+
219+
let originalEmail = "rtbo"
220+
let originalToken = "hngk"
221+
222+
let updatedEmail = "2"
223+
let updatedToken = "564g"
224+
225+
let authDelegate = DefaultAuthDelegate {
226+
return originalToken
227+
}
228+
229+
let config = IterableConfig()
230+
config.authDelegate = authDelegate
231+
232+
internalAPI = InternalIterableAPI.initializeForTesting(config: config)
233+
234+
guard let API = internalAPI else {
235+
XCTFail()
236+
return
237+
}
238+
239+
API.setEmail(originalEmail)
240+
241+
XCTAssertEqual(API.email, originalEmail)
242+
XCTAssertNil(API.userId)
243+
XCTAssertEqual(API.auth.authToken, originalToken)
244+
245+
API.updateEmail(updatedEmail, withToken: updatedToken) { data in
246+
XCTAssertEqual(API.email, updatedEmail)
247+
XCTAssertNil(API.userId)
248+
XCTAssertEqual(API.auth.authToken, updatedToken)
249+
250+
condition1.fulfill()
251+
} onFailure: { reason, data in
252+
XCTFail()
253+
}
254+
255+
wait(for: [condition1], timeout: testExpectationTimeout)
256+
}
257+
214258
func testLogoutUser() {
215259
let authDelegate = createStockAuthDelegate()
216260

0 commit comments

Comments
 (0)