Skip to content

Commit 3deff0a

Browse files
authored
Merge pull request #463 from Iterable/MOB-4641-Add-Success-and-failure-jwt-callbacks
[MOB-4641] - Add success and failure callback to AuthHandler
2 parents 651fa05 + 8439966 commit 3deff0a

File tree

3 files changed

+39
-34
lines changed

3 files changed

+39
-34
lines changed

iterableapi/src/main/java/com/iterable/iterableapi/IterableApi.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ void setNotificationData(IterableNotificationData data) {
220220
}
221221
}
222222

223-
void setAuthToken(String authToken) {
223+
public void setAuthToken(String authToken) {
224224
setAuthToken(authToken, false);
225225
}
226226

@@ -351,7 +351,9 @@ public void setEmail(@Nullable String email) {
351351
}
352352

353353
public void setEmail(@Nullable String email, @Nullable String authToken) {
354+
//Only if passed in same non-null email
354355
if (_email != null && _email.equals(email)) {
356+
checkAndUpdateAuthToken(authToken);
355357
return;
356358
}
357359

@@ -380,7 +382,9 @@ public void setUserId(@Nullable String userId) {
380382
}
381383

382384
public void setUserId(@Nullable String userId, @Nullable String authToken) {
385+
//If same non null userId is passed
383386
if (_userId != null && _userId.equals(userId)) {
387+
checkAndUpdateAuthToken(authToken);
384388
return;
385389
}
386390

@@ -397,6 +401,13 @@ public void setUserId(@Nullable String userId, @Nullable String authToken) {
397401
onLogin(authToken);
398402
}
399403

404+
private void checkAndUpdateAuthToken(@Nullable String authToken) {
405+
// If authHandler exists and if authToken is new, it will be considered as a call to update the authToken.
406+
if (config.authHandler != null && authToken != null && authToken != _authToken) {
407+
setAuthToken(authToken);
408+
}
409+
}
410+
400411
/**
401412
* Tracks a click on the uri if it is an iterable link.
402413
* @param uri the

iterableapi/src/main/java/com/iterable/iterableapi/IterableAuthHandler.java

+2
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
public interface IterableAuthHandler {
44
String onAuthTokenRequested();
5+
void onTokenRegistrationSuccessful(String authToken);
6+
void onTokenRegistrationFailed(Throwable object);
57
}

iterableapi/src/main/java/com/iterable/iterableapi/IterableAuthManager.java

+25-33
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import android.util.Base64;
44

5-
import androidx.annotation.Nullable;
65
import androidx.annotation.VisibleForTesting;
76

87
import com.iterable.iterableapi.util.Future;
@@ -35,44 +34,37 @@ public class IterableAuthManager {
3534
}
3635

3736
public synchronized void requestNewAuthToken(boolean hasFailedPriorAuth) {
38-
requestNewAuthToken(hasFailedPriorAuth, null);
39-
}
40-
41-
public synchronized void requestNewAuthToken(boolean hasFailedPriorAuth, @Nullable final IterableHelper.SuccessAuthHandler onSuccess) {
4237
if (authHandler != null) {
4338
if (!pendingAuth) {
4439
if (!(this.hasFailedPriorAuth && hasFailedPriorAuth)) {
4540
this.hasFailedPriorAuth = hasFailedPriorAuth;
4641
pendingAuth = true;
4742
Future.runAsync(new Callable<String>() {
48-
@Override
49-
public String call() throws Exception {
50-
return authHandler.onAuthTokenRequested();
51-
}
52-
}).onSuccess(new Future.SuccessCallback<String>() {
53-
@Override
54-
public void onSuccess(String authToken) {
55-
if (authToken != null) {
56-
queueExpirationRefresh(authToken);
57-
}
58-
59-
IterableApi.getInstance().setAuthToken(authToken);
60-
pendingAuth = false;
61-
reSyncAuth();
62-
63-
if (onSuccess != null) {
64-
onSuccess(authToken);
65-
}
66-
}
67-
})
68-
.onFailure(new Future.FailureCallback() {
69-
@Override
70-
public void onFailure(Throwable throwable) {
71-
IterableLogger.e(TAG, "Error while requesting Auth Token", throwable);
72-
pendingAuth = false;
73-
reSyncAuth();
74-
}
75-
});
43+
@Override
44+
public String call() throws Exception {
45+
return authHandler.onAuthTokenRequested();
46+
}
47+
}).onSuccess(new Future.SuccessCallback<String>() {
48+
@Override
49+
public void onSuccess(String authToken) {
50+
if (authToken != null) {
51+
queueExpirationRefresh(authToken);
52+
}
53+
IterableApi.getInstance().setAuthToken(authToken);
54+
pendingAuth = false;
55+
reSyncAuth();
56+
authHandler.onTokenRegistrationSuccessful(authToken);
57+
}
58+
})
59+
.onFailure(new Future.FailureCallback() {
60+
@Override
61+
public void onFailure(Throwable throwable) {
62+
IterableLogger.e(TAG, "Error while requesting Auth Token", throwable);
63+
authHandler.onTokenRegistrationFailed(throwable);
64+
pendingAuth = false;
65+
reSyncAuth();
66+
}
67+
});
7668
}
7769
} else if (!hasFailedPriorAuth) {
7870
//setFlag to resync auth after current auth returns

0 commit comments

Comments
 (0)