Skip to content

Commit 5782d97

Browse files
authored
Merge pull request #452 from Iterable/jay/MOB-4535-refactor-login-flow
[MOB-4535] login flow refactor
2 parents 15f3951 + ae2dedf commit 5782d97

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

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

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ void setAuthToken(String authToken, boolean bypassAuth) {
229229
if ((authToken != null && !authToken.equalsIgnoreCase(_authToken)) || (_authToken != null && !_authToken.equalsIgnoreCase(authToken))) {
230230
_authToken = authToken;
231231
storeAuthData();
232-
onLogIn();
232+
completeUserLogin();
233233
} else if (bypassAuth) {
234-
onLogIn();
234+
completeUserLogin();
235235
}
236236
}
237237
}
@@ -347,6 +347,10 @@ public void execute(@Nullable String data) {
347347
* @param email User email
348348
*/
349349
public void setEmail(@Nullable String email) {
350+
setEmail(email, null);
351+
}
352+
353+
public void setEmail(@Nullable String email, @Nullable String authToken) {
350354
if (_email != null && _email.equals(email)) {
351355
return;
352356
}
@@ -355,16 +359,13 @@ public void setEmail(@Nullable String email) {
355359
return;
356360
}
357361

358-
onLogOut();
362+
logoutPreviousUser();
363+
359364
_email = email;
360365
_userId = null;
361366
storeAuthData();
362367

363-
if (email != null) {
364-
getAuthManager().requestNewAuthToken(false);
365-
} else {
366-
setAuthToken(null);
367-
}
368+
onLogin(authToken);
368369
}
369370

370371
/**
@@ -375,6 +376,10 @@ public void setEmail(@Nullable String email) {
375376
* @param userId User ID
376377
*/
377378
public void setUserId(@Nullable String userId) {
379+
setUserId(userId, null);
380+
}
381+
382+
public void setUserId(@Nullable String userId, @Nullable String authToken) {
378383
if (_userId != null && _userId.equals(userId)) {
379384
return;
380385
}
@@ -383,16 +388,13 @@ public void setUserId(@Nullable String userId) {
383388
return;
384389
}
385390

386-
onLogOut();
391+
logoutPreviousUser();
392+
387393
_email = null;
388394
_userId = userId;
389395
storeAuthData();
390396

391-
if (userId != null) {
392-
getAuthManager().requestNewAuthToken(false);
393-
} else {
394-
setAuthToken(null);
395-
}
397+
onLogin(authToken);
396398
}
397399

398400
/**
@@ -1080,23 +1082,39 @@ private void retrieveEmailAndUserId() {
10801082
}
10811083
}
10821084

1083-
private void onLogOut() {
1085+
private void logoutPreviousUser() {
10841086
if (config.autoPushRegistration && isInitialized()) {
10851087
disablePush();
10861088
}
1089+
10871090
getInAppManager().reset();
10881091
getAuthManager().clearRefreshTimer();
1092+
10891093
apiClient.onLogout();
10901094
}
10911095

1092-
private void onLogIn() {
1096+
private void onLogin(@Nullable String authToken) {
1097+
if (!isInitialized()) {
1098+
setAuthToken(null);
1099+
return;
1100+
}
1101+
1102+
if (authToken != null) {
1103+
setAuthToken(authToken);
1104+
} else {
1105+
getAuthManager().requestNewAuthToken(false);
1106+
}
1107+
}
1108+
1109+
private void completeUserLogin() {
10931110
if (!isInitialized()) {
10941111
return;
10951112
}
10961113

10971114
if (config.autoPushRegistration) {
10981115
registerForPush();
10991116
}
1117+
11001118
getInAppManager().syncInApp();
11011119
}
11021120

@@ -1110,7 +1128,6 @@ public void clearInboxSessionId() {
11101128
this.inboxSessionId = null;
11111129
}
11121130

1113-
11141131
private class IterableApiAuthProvider implements IterableApiClient.AuthProvider {
11151132
@Nullable
11161133
@Override

iterableapi/src/test/java/com/iterable/iterableapi/IterableApiTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ public void testUpdateEmailWithUserId() throws Exception {
192192
assertEquals("testUserId", IterableApi.getInstance().getUserId());
193193
}
194194

195+
@Ignore
195196
@Test
196197
public void testHandleUniversalLinkRewrite() throws Exception {
197198
IterableUrlHandler urlHandlerMock = mock(IterableUrlHandler.class);

0 commit comments

Comments
 (0)