Skip to content

Commit 1008bc5

Browse files
committed
chore(auth): ensure can route back to login after handling login deeplinks
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 63644b1 commit 1008bc5

3 files changed

Lines changed: 42 additions & 26 deletions

File tree

app/src/main/java/com/getcode/CodeApp.kt

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@ fun CodeApp() {
5353
backgroundColor = Brand,
5454
scaffoldState = appState.scaffoldState
5555
) { innerPaddingModifier ->
56+
val codeNavigator = LocalCodeNavigator.current
57+
var replacingStackFromDeepLink by remember {
58+
mutableStateOf(false)
59+
}
60+
5661
Navigator(
5762
screen = MainRoot,
5863
) { navigator ->
59-
val codeNavigator = LocalCodeNavigator.current
6064
appState.navigator = codeNavigator
6165

6266
LaunchedEffect(navigator.lastItem) {
@@ -65,10 +69,6 @@ fun CodeApp() {
6569
codeNavigator.screensNavigator = navigator
6670
}
6771

68-
var replacingStackFromDeepLink by remember {
69-
mutableStateOf(false)
70-
}
71-
7272
val (isVisibleTopBar, isVisibleBackButton) = appState.isVisibleTopBar
7373
if (isVisibleTopBar && appState.currentTitle.isNotBlank()) {
7474
TitleBar(
@@ -97,23 +97,23 @@ fun CodeApp() {
9797
}
9898
}
9999
}
100+
}
100101

101-
//Listen for authentication changes here
102-
AuthCheck(
103-
navigator = codeNavigator,
104-
onNavigate = { screens, fromDeeplink ->
105-
replacingStackFromDeepLink = fromDeeplink
106-
codeNavigator.replaceAll(screens, inSheet = false)
107-
},
108-
onSwitchAccounts = { seed ->
109-
activity?.let {
110-
tlvm.logout(it) {
111-
appState.navigator.replaceAll(LoginScreen(seed))
112-
}
102+
//Listen for authentication changes here
103+
AuthCheck(
104+
navigator = codeNavigator,
105+
onNavigate = { screens, fromDeeplink ->
106+
replacingStackFromDeepLink = fromDeeplink
107+
codeNavigator.replaceAll(screens, inSheet = false)
108+
},
109+
onSwitchAccounts = { seed ->
110+
activity?.let {
111+
tlvm.logout(it) {
112+
appState.navigator.replaceAll(LoginScreen(seed))
113113
}
114114
}
115-
)
116-
}
115+
}
116+
)
117117
}
118118
}
119119

app/src/main/java/com/getcode/manager/AuthManager.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,6 @@ class AuthManager @Inject constructor(
154154
AccountUtils.removeAccounts(activity)
155155
.doOnSuccess { res: Boolean ->
156156
if (res) {
157-
// reset session state
158-
SessionManager.update { SessionManager.SessionState() }
159157
clearToken()
160158
onComplete()
161159
}
@@ -167,8 +165,6 @@ class AuthManager @Inject constructor(
167165
AccountUtils.removeAccounts(activity)
168166
.doOnSuccess { success ->
169167
if (success) {
170-
// reset session state
171-
SessionManager.update { SessionManager.SessionState() }
172168
clearToken()
173169
cont.resume(Result.success(Unit))
174170
}

app/src/main/java/com/getcode/view/components/AuthCheck.kt

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.compose.runtime.collectAsState
77
import androidx.compose.runtime.getValue
88
import androidx.compose.runtime.mutableStateOf
99
import androidx.compose.runtime.remember
10+
import androidx.compose.runtime.rememberCoroutineScope
1011
import androidx.compose.runtime.setValue
1112
import androidx.compose.ui.platform.LocalContext
1213
import cafe.adriel.voyager.core.screen.Screen
@@ -21,11 +22,13 @@ import com.getcode.navigation.screens.LoginGraph
2122
import com.getcode.navigation.screens.LoginScreen
2223
import com.getcode.util.DeeplinkHandler
2324
import com.getcode.util.getActivity
25+
import kotlinx.coroutines.delay
2426
import kotlinx.coroutines.flow.filter
2527
import kotlinx.coroutines.flow.filterNotNull
2628
import kotlinx.coroutines.flow.launchIn
2729
import kotlinx.coroutines.flow.mapNotNull
2830
import kotlinx.coroutines.flow.onEach
31+
import kotlinx.coroutines.launch
2932
import timber.log.Timber
3033

3134
const val AUTH_NAV = "Authentication Navigation"
@@ -47,6 +50,7 @@ fun AuthCheck(
4750
}
4851

4952
LaunchedEffect(isAuthenticated) {
53+
Timber.tag(AUTH_NAV).d("authenticated=$isAuthenticated")
5054
isAuthenticated?.let { authenticated ->
5155
//Allow the seed input screen to complete and avoid
5256
//premature navigation
@@ -72,7 +76,9 @@ fun AuthCheck(
7276

7377
val context = LocalContext.current
7478
deeplinkHandler ?: return
79+
7580
LaunchedEffect(deeplinkHandler) {
81+
val scope = this
7682
deeplinkHandler.intent
7783
.filterNotNull()
7884
.onEach {
@@ -96,7 +102,20 @@ fun AuthCheck(
96102
deeplinkRouted = true
97103
context.getActivity()?.intent = null
98104
deeplinkHandler.debounceIntent = null
99-
showLogoutMessage(context, entropy, onSwitchAccounts)
105+
showLogoutMessage(
106+
context = context,
107+
entropyB64 = entropy,
108+
onSwitchAccounts = {
109+
scope.launch {
110+
delay(300)
111+
onSwitchAccounts(it)
112+
deeplinkRouted = false
113+
}
114+
},
115+
onCancel = {
116+
deeplinkRouted = false
117+
}
118+
)
100119
return@mapNotNull null
101120
}
102121
}
@@ -117,7 +136,8 @@ fun AuthCheck(
117136
private fun showLogoutMessage(
118137
context: Context,
119138
entropyB64: String,
120-
onSwitchAccounts: (String) -> Unit
139+
onSwitchAccounts: (String) -> Unit,
140+
onCancel: () -> Unit,
121141
) {
122142
BottomBarManager.showMessage(
123143
BottomBarManager.BottomBarMessage(
@@ -129,7 +149,7 @@ private fun showLogoutMessage(
129149
onPositive = {
130150
onSwitchAccounts(entropyB64)
131151
},
132-
onNegative = { }
152+
onNegative = { onCancel() }
133153
)
134154
)
135155
}

0 commit comments

Comments
 (0)