Android SDK
Authentication
Cartesian uses token-only auth. Your backend signs the shopper in and the SDK receives a session token.
The SDK never asks for a password and never sees a shopper's credentials. Your app already knows who the shopper is. Your backend issues a Cartesian session token for that shopper, and your app passes it to the SDK.
Shopper signs in to your app ──► your backend ──► Cartesian backend (your infra)
│ issues a Cartesian session token
your app ◄────────────────────────────┘
└─ Cartesian.client.signIn(token)textSign in
lifecycleScope.launch {
try {
val user = Cartesian.client.signIn(sessionToken)
Log.d("Cartesian", "Signed in as ${user.displayName}")
} catch (e: CartesianException.Unauthorized) {
// token rejected: get a fresh one from your backend
} catch (e: CartesianException.Network) {
// offline: the previous session (if any) is kept, try again later
}
}kotlinsignIn checks the token against GET /api/auth/session, saves it, connects the socket and loads the cart, wishlist and threads.
Restoring on launch
The client restores the saved token by itself. Watch authState to see the result:
Cartesian.client.authState.collect { state ->
when (state) {
AuthState.Restoring -> showSpinner()
AuthState.SignedOut -> askYourBackendForAToken()
is AuthState.SignedIn -> hideSpinner()
}
}kotlin| State | Meaning |
|---|---|
Restoring | A saved token is being checked. This includes being offline: the token is kept and checked again when the network comes back. |
SignedOut | There is no token, or the backend rejected it with 401. |
SignedIn(user) | Ready. user.id, user.email, user.displayName. |
Network errors and 5xx responses never sign the shopper out. Only a 401 clears the session, so a flaky connection won't send people back to a sign-in prompt.
Let the UI ask you for a token
When the assistant UI is shown and the client is SignedOut while the device is online, the SDK calls CartesianListener.onSignInRequired(). Use it to fetch a token silently, or to send the shopper to your own sign-in screen:
override fun onSignInRequired() {
appScope.launch {
val token = myBackend.cartesianToken() ?: return@launch
Cartesian.client.signIn(token)
}
}kotlinSign out
Call this from your own sign-out flow:
Cartesian.client.signOut() // POST /api/auth/logout, then clears the stored tokenkotlinCustom token storage
Cartesian.initialize stores the token in a private SharedPreferences file. If you construct CartesianClient yourself (headless), pass any SessionStore, for example one backed by EncryptedSharedPreferences or the Keystore:
class SecureStore(private val prefs: SharedPreferences) : SessionStore {
override fun load(key: String) = prefs.getString(key, null)
override fun save(key: String, token: String) = prefs.edit { putString(key, token) }
override fun clear(key: String) = prefs.edit { remove(key) }
}kotlin