Skip to content

Commit 2bdee54

Browse files
committed
Multi accounts - handle login link when there is already an account.
1 parent 4e34300 commit 2bdee54

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

appnav/src/main/kotlin/io/element/android/appnav/RootFlowNode.kt

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -299,19 +299,25 @@ class RootFlowNode(
299299
}
300300

301301
private suspend fun onLoginLink(params: LoginParams) {
302-
// Is there a session already?
303-
val latestSessionId = authenticationService.getLatestSessionId()
304-
if (latestSessionId == null) {
305-
// No session, open login
306-
if (accountProviderAccessControl.isAllowedToConnectToAccountProvider(params.accountProvider.ensureProtocol())) {
307-
switchToNotLoggedInFlow(params)
302+
if (accountProviderAccessControl.isAllowedToConnectToAccountProvider(params.accountProvider.ensureProtocol())) {
303+
// Is there a session already?
304+
val sessions = sessionStore.getAllSessions()
305+
if (sessions.isNotEmpty()) {
306+
val loginHintMatrixId = params.loginHint?.removePrefix("mxid:")
307+
val existingAccount = sessions.find { it.userId == loginHintMatrixId }
308+
if (existingAccount != null) {
309+
// We have an existing account matching the login hint, ensure this is the current session
310+
sessionStore.setLatestSession(existingAccount.userId)
311+
} else {
312+
val latestSessionId = sessions.maxBy { it.lastUsageIndex }.userId
313+
attachSession(SessionId(latestSessionId))
314+
backstack.push(NavTarget.NotLoggedInFlow(params))
315+
}
308316
} else {
309-
Timber.w("Login link ignored, we are not allowed to connect to the homeserver")
310-
switchToNotLoggedInFlow(null)
317+
switchToNotLoggedInFlow(params)
311318
}
312319
} else {
313-
// Just ignore the login link if we already have a session
314-
Timber.w("Login link ignored, we already have a session")
320+
Timber.w("Login link ignored, we are not allowed to connect to the homeserver")
315321
}
316322
}
317323

@@ -322,6 +328,7 @@ class RootFlowNode(
322328
// No session, open login
323329
switchToNotLoggedInFlow(null)
324330
} else {
331+
// TODO Multi-account: show a screen to select an account
325332
attachSession(latestSessionId)
326333
.attachIncomingShare(intent)
327334
}

0 commit comments

Comments
 (0)