Modern Apps git

← Modern-Apps.git

fooddelivery: handle email verification links

Commit
a6cce70965b9e3f6cab2eda0adcd172d55995ba2
Author
Vayun Mathur <[email protected]>
When
2026-11-01 06:16 UTC (1 month ago)
Parents
8953cbb
Accepts the two forms the official Bites app does:
- https://withbites.com/email-verification/{customerId}/{token}
- com.deliverycollective.storefront://email-verification/{customerId}/{token}
  (the hand-off the withbites.com verify page uses)

Opens a new EmailVerification route that posts to
/customers/verify-email/{customerId}/{token} and shows a loading,
success or error state with retry. The https filter can't be an
auto-verified App Link without assetlinks on withbites.com, so it
appears in the "Open with" sheet. Link segments are restricted to
[A-Za-z0-9_-] because they go into the API path.

Diff

fooddelivery/src/main/AndroidManifest.xml
diff --git a/fooddelivery/src/main/AndroidManifest.xml b/fooddelivery/src/main/AndroidManifest.xmlindex 9dd14c0681..ab05653f50 100644--- a/fooddelivery/src/main/AndroidManifest.xml+++ b/fooddelivery/src/main/AndroidManifest.xml@@ -1,5 +1,6 @@ <?xml version="1.0" encoding="utf-8"?>-<manifest xmlns:android="http://schemas.android.com/apk/res/android">+<manifest xmlns:android="http://schemas.android.com/apk/res/android"+    xmlns:tools="http://schemas.android.com/tools">      <uses-permission android:name="android.permission.INTERNET" />     <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />@@ -36,6 +37,25 @@                 <category android:name="android.intent.category.BROWSABLE" />                 <data android:scheme="fooddelivery" android:host="restaurant" />             </intent-filter>+            <!-- Email verification links, in both forms the official Bites app accepts: the+                 custom scheme the withbites.com verify page hands off to, and the web link in+                 the email itself. Not autoVerify — we can't serve assetlinks on withbites.com,+                 so the https form shows up as a choice in the "Open with" sheet. -->+            <intent-filter>+                <action android:name="android.intent.action.VIEW" />+                <category android:name="android.intent.category.DEFAULT" />+                <category android:name="android.intent.category.BROWSABLE" />+                <data android:scheme="com.deliverycollective.storefront" android:host="email-verification" />+            </intent-filter>+            <intent-filter tools:ignore="AppLinkWarning">+                <action android:name="android.intent.action.VIEW" />+                <category android:name="android.intent.category.DEFAULT" />+                <category android:name="android.intent.category.BROWSABLE" />+                <data android:scheme="https" />+                <data android:host="withbites.com" />+                <data android:host="www.withbites.com" />+                <data android:pathPrefix="/email-verification/" />+            </intent-filter>         </activity>          <activity
fooddelivery/src/main/java/com/vayunmathur/fooddelivery/MainActivity.kt
diff --git a/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/MainActivity.kt b/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/MainActivity.ktindex 01fe8f0aa1..7644f0bc82 100644--- a/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/MainActivity.kt+++ b/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/MainActivity.kt@@ -40,6 +40,7 @@ import com.vayunmathur.fooddelivery.ui.AccountScreen import com.vayunmathur.fooddelivery.ui.CartScreen import com.vayunmathur.fooddelivery.ui.CheckoutScreen import com.vayunmathur.fooddelivery.ui.DealsScreen+import com.vayunmathur.fooddelivery.ui.EmailVerificationScreen import com.vayunmathur.fooddelivery.ui.HomeScreen import com.vayunmathur.fooddelivery.ui.OrderTrackingScreen import com.vayunmathur.fooddelivery.ui.OrdersScreen@@ -62,6 +63,7 @@ sealed interface Route : NavKey {     @Serializable data class Restaurant(val id: Int) : Route     @Serializable data object Checkout : Route     @Serializable data class OrderTracking(val orderId: Int) : Route+    @Serializable data class EmailVerification(val customerId: String, val token: String) : Route }  class MainActivity : MaActivity() {@@ -74,6 +76,8 @@ class MainActivity : MaActivity() {     // onNewIntent can re-deep-link into the running UI.     private val openRestaurantId = mutableStateOf<Int?>(null) +    private val verifyEmail = mutableStateOf<Route.EmailVerification?>(null)+     private val requestNotificationPermission =         registerForActivityResult(ActivityResultContracts.RequestPermission()) { } @@ -88,6 +92,7 @@ class MainActivity : MaActivity() {         AppInit.start(this)         trackOrderId.value = intent.trackOrderIdOrNull()         openRestaurantId.value = intent.restaurantIdOrNull()+        verifyEmail.value = intent.emailVerificationOrNull()         requestNotificationPermissionIfNeeded()         // The TLS trust bundle and the saved auth token are warmed up off the main thread by         // AppInit, so nothing here touches the network or disk.@@ -100,7 +105,7 @@ class MainActivity : MaActivity() {      @Composable     override fun Content() {-        FoodDeliveryApp(trackOrderId, openRestaurantId)+        FoodDeliveryApp(trackOrderId, openRestaurantId, verifyEmail)     }      override fun onNewIntent(intent: Intent) {@@ -108,6 +113,7 @@ class MainActivity : MaActivity() {         setIntent(intent)         intent.trackOrderIdOrNull()?.let { trackOrderId.value = it }         intent.restaurantIdOrNull()?.let { openRestaurantId.value = it }+        intent.emailVerificationOrNull()?.let { verifyEmail.value = it }     }      private fun requestNotificationPermissionIfNeeded() {@@ -130,8 +136,35 @@ private fun Intent.restaurantIdOrNull(): Int? {     return uri.lastPathSegment?.toIntOrNull()?.takeIf { it > 0 } } +private const val EMAIL_VERIFICATION = "email-verification"++// Both segments end up in an API path, so only plain id characters are let through.+private val LINK_SEGMENT = Regex("[A-Za-z0-9_-]{1,256}")++/**+ * com.deliverycollective.storefront://email-verification/<customerId>/<token> or+ * https://withbites.com/email-verification/<customerId>/<token> — the same two forms the+ * official Bites app handles. Returns null for anything else.+ */+private fun Intent.emailVerificationOrNull(): Route.EmailVerification? {+    val uri = data ?: return null+    val segments = when {+        uri.scheme == "com.deliverycollective.storefront" && uri.host == EMAIL_VERIFICATION ->+            uri.pathSegments+        uri.scheme == "https" && uri.pathSegments.firstOrNull() == EMAIL_VERIFICATION ->+            uri.pathSegments.drop(1)+        else -> return null+    }+    if (segments.size != 2 || !segments.all { LINK_SEGMENT.matches(it) }) return null+    return Route.EmailVerification(customerId = segments[0], token = segments[1])+}+ @Composable-private fun FoodDeliveryApp(trackOrderId: MutableState<Int?>, openRestaurantId: MutableState<Int?>) {+private fun FoodDeliveryApp(+    trackOrderId: MutableState<Int?>,+    openRestaurantId: MutableState<Int?>,+    verifyEmail: MutableState<Route.EmailVerification?>,+) {     val context = LocalContext.current     val backStack = rememberNavBackStack<Route>(Route.Main()) @@ -148,6 +181,12 @@ private fun FoodDeliveryApp(trackOrderId: MutableState<Int?>, openRestaurantId:         backStack.add(Route.Restaurant(id))         openRestaurantId.value = null     }++    LaunchedEffect(verifyEmail.value) {+        val route = verifyEmail.value ?: return@LaunchedEffect+        backStack.add(route)+        verifyEmail.value = null+    }     // Restoring the cart is a prefs read plus a JSON decode, so it happens off the main     // thread rather than in the remember initialiser. Anything added while the read was in     // flight stays in the cart, and is re-persisted alongside the restored lines.@@ -195,6 +234,13 @@ private fun FoodDeliveryApp(trackOrderId: MutableState<Int?>, openRestaurantId:         entry<Route.OrderTracking>(metadata = ListDetailPage() + MorphPage()) { route ->             OrderTrackingScreen(orderId = route.orderId, onBack = { backStack.pop() })         }+        entry<Route.EmailVerification>(metadata = ListDetailPage()) { route ->+            EmailVerificationScreen(+                customerId = route.customerId,+                token = route.token,+                onDone = { backStack.pop() },+            )+        }     } } 
fooddelivery/src/main/java/com/vayunmathur/fooddelivery/ui/EmailVerificationScreen.kt
diff --git a/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/ui/EmailVerificationScreen.kt b/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/ui/EmailVerificationScreen.ktnew file mode 100644index 0000000000..f3564e6c94--- /dev/null+++ b/fooddelivery/src/main/java/com/vayunmathur/fooddelivery/ui/EmailVerificationScreen.kt@@ -0,0 +1,124 @@+package com.vayunmathur.fooddelivery.ui++import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.Spacer+import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.foundation.layout.height+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.size+import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableIntStateOf+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.res.stringResource+import androidx.compose.ui.text.style.TextAlign+import androidx.compose.ui.unit.dp+import com.vayunmathur.fooddelivery.R+import com.vayunmathur.fooddelivery.api.BitesCustomers+import com.vayunmathur.fooddelivery.platform.AppInit+import com.vayunmathur.library.ui.AppScaffold+import com.vayunmathur.library.ui.Button+import com.vayunmathur.library.ui.CircularProgressIndicator+import com.vayunmathur.library.ui.IconCheck+import com.vayunmathur.library.ui.IconWarning+import com.vayunmathur.library.ui.MaterialTheme+import com.vayunmathur.library.ui.R as UiR+import com.vayunmathur.library.ui.Text+import com.vayunmathur.library.ui.TextButton+import com.vayunmathur.library.ui.appBarScrollBehavior++private enum class VerifyState { LOADING, SUCCESS, ERROR }++/** Opened from an email-verification link; posts the link's customer id + token. */+@Composable+internal fun EmailVerificationScreen(+    customerId: String,+    token: String,+    onDone: () -> Unit,+) {+    var state by remember { mutableStateOf(VerifyState.LOADING) }+    var attempt by remember { mutableIntStateOf(0) }++    LaunchedEffect(customerId, token, attempt) {+        state = VerifyState.LOADING+        AppInit.awaitReady()+        state = if (BitesCustomers.verifyEmailToken(customerId, token)) {+            VerifyState.SUCCESS+        } else {+            VerifyState.ERROR+        }+    }++    EmailVerificationContent(+        state = state,+        onRetry = { attempt++ },+        onDone = onDone,+    )+}++@Composable+private fun EmailVerificationContent(+    state: VerifyState,+    onRetry: () -> Unit,+    onDone: () -> Unit,+) {+    AppScaffold(+        title = stringResource(R.string.email_verification),+        onClose = onDone,+        scrollBehavior = appBarScrollBehavior(),+    ) { padding ->+        Column(+            Modifier.fillMaxSize().padding(padding).padding(24.dp),+            horizontalAlignment = Alignment.CenterHorizontally,+            verticalArrangement = Arrangement.Center,+        ) {+            when (state) {+                VerifyState.LOADING -> {+                    CircularProgressIndicator()+                    Spacer(Modifier.height(16.dp))+                    Text(stringResource(R.string.verifying_your_email))+                }+                VerifyState.SUCCESS -> {+                    IconCheck(+                        modifier = Modifier.size(64.dp),+                        tint = MaterialTheme.colorScheme.primary,+                    )+                    Spacer(Modifier.height(16.dp))+                    Text(+                        stringResource(R.string.email_verified),+                        style = MaterialTheme.typography.headlineSmall,+                    )+                    Spacer(Modifier.height(24.dp))+                    Button(onClick = onDone) { Text(stringResource(UiR.string.done)) }+                }+                VerifyState.ERROR -> {+                    IconWarning(+                        modifier = Modifier.size(64.dp),+                        tint = MaterialTheme.colorScheme.error,+                    )+                    Spacer(Modifier.height(16.dp))+                    Text(+                        stringResource(R.string.email_verification_failed),+                        style = MaterialTheme.typography.headlineSmall,+                    )+                    Spacer(Modifier.height(8.dp))+                    Text(+                        stringResource(R.string.email_verification_failed_detail),+                        style = MaterialTheme.typography.bodyMedium,+                        color = MaterialTheme.colorScheme.onSurfaceVariant,+                        textAlign = TextAlign.Center,+                    )+                    Spacer(Modifier.height(24.dp))+                    Button(onClick = onRetry) { Text(stringResource(UiR.string.retry)) }+                    TextButton(onClick = onDone) { Text(stringResource(UiR.string.close)) }+                }+            }+        }+    }+}
fooddelivery/src/main/res/values/strings.xml
diff --git a/fooddelivery/src/main/res/values/strings.xml b/fooddelivery/src/main/res/values/strings.xmlindex 685f31554a..fbb477243b 100644--- a/fooddelivery/src/main/res/values/strings.xml+++ b/fooddelivery/src/main/res/values/strings.xml@@ -105,4 +105,8 @@     <string name="order_tracking_channel_desc">Ongoing updates while your order is on the way</string>     <!-- %1$s = status label e.g. Driving to you, %2$d = minutes remaining -->     <string name="notif_status_eta">%1$s · %2$d min</string>+    <string name="verifying_your_email">Verifying your email address…</string>+    <string name="email_verified">Email verified</string>+    <string name="email_verification_failed">Email verification failed</string>+    <string name="email_verification_failed_detail">This link may have expired or already been used. Request a new verification email and try again.</string> </resources>