Skip to content

Sign users in

Your app signs its users in; the SDK never does, and draws no sign-in screen. Every request names your app by its key, and the user by a token. PurrCRM offers three ways to sign users in, all on Settings › your app › Developers › Sign-in. An app can use more than one at once, for example while it moves from one to another.

The SDK keeps tokens in memory only. It never writes one to the phone.

  1. On Settings › your app › Developers › Sign-in, in the Identity secret card, press Make a secret (crm_is_…). It is shown once. Keep it in your backend’s secrets, never in the app.
  2. Your backend signs a short token for the signed-in user with it.
  3. Your app asks your backend for the token, and hands it to the SDK.

Sign an HS256 token with the user’s id as sub, and an exp minutes or hours away (more than 30 days is refused). Include iat too. If the Your own sign-in (key set) card on Settings › your app › Developers › Sign-in names an issuer, an audience or a kind, add them as iss, aud and knd. Serve the token to your app from a route of your own, such as /crm-token, for the user signed in.

The code, in Node, Python, Go, PHP and Ruby, and a route that serves it, is on Sign users in under Web app and backend: the token is the same for an app and a website.

The user’s id must be the same one your backend uses for them in Identify, so that what the app and the backend say about a person ends up on one contact.

CrmTokens.fetched asks your backend for the token whenever the SDK needs one:

NorthwindApp.kt
val tokens = CrmTokens.fetched {
// A suspend function: call your backend, signed in as your user.
val answer = backend.get("/crm-token") ?: return@fetched null // null: nobody is signed in
CrmFetchedToken(answer.getString("token"))
}
crm = Crm(this, config, CrmAuth.tokens(tokens), host)

It keeps the token until a minute before its exp (half its life, for one shorter than that), asks once for every request waiting on one, and forgets a token PurrCRM refused as expired, so the next request asks for a new one. A phone whose clock is wrong does not make it ask for every request: it times the token by its own exp less iat, which is why you should sign an iat. If your backend says when the token expires, pass it: CrmFetchedToken(token, expiresAt = epochMillis). CrmTokens.fetched(earlierMs = ...) changes the minute.

Two secrets can work at once. Make a new one, deploy your backend with it, watch the old one’s Last used stop moving, then Revoke it. Revoke a secret that may have leaked at once: its users are refused until your backend signs with the new one.

When your user has signed in, and is past your own first steps (onboarding, permission screens):

crm.session(CrmSession(guestId = user.id, displayName = user.name))
crm.setReady(true)

When they sign out:

lifecycleScope.launch {
crm.forgetPushToken() // while their token still works, see Push
tokens.forget()
crm.setReady(false)
crm.session(null)
}
  • guestId is the user’s id in your app (the token’s sub). displayName is what you call them, for a greeting.
  • Until setReady(true), no in-app message is offered, and a tapped push’s page or a link’s place waits. Call it again whenever that changes. The setup check warns when setReady(true) comes more than 10 seconds after the SDK was made.
  • CrmSession(productEventsOff = true) records no product events for a user who turned them off in your settings, and drops what was queued for them (Events).
  • CrmSession(features = setOf(...)) holds some parts back from this user (Initialise).
  • crm.session is a StateFlow<CrmSession?> of who is signed in, with the parts they have; crm.ready is a StateFlow<Boolean>.

On sign-out the SDK forgets what was the user’s: their conversations, inbox and in-app messages, and a link or tapped push still waiting. It also forgets the phone’s push token, sent as the user its last request went as, so this works even when tokens.forget() came first, as long as that token is still good. Calling forgetPushToken() yourself first, as above, is what makes sure.

Your app can let people write to your team before they sign in, as visitors. Your team turns it on in the Website visitors card on Settings › your app › Developers › Sign-in: Let website visitors write before they sign in, then Also let people using your phone app write before they sign in (Let people write before they sign in). With the phone app’s switch on, anyone who has your app’s public key can start a chat without signing in.

With the built-in screens, there is nothing more to wire. With nobody signed in, open the messenger from a button of your own:

// A "Chat with us" button on your signed-out screen.
chatButton.setOnClickListener { ui.presentMessenger() }

The SDK then makes a visitor, keeps it in the Crm’s store, and the person writes as that visitor. A visitor is only made when the messenger or a new conversation opens, never when the app starts. The launcher waits until there is someone to write as, so the first open is always your button’s. While nobody is signed in, crm.session is the visitor’s, and crm.visitorId is its id.

  • When the person signs in (crm.session(...)), the visitor is merged into them first: their conversations become the user’s.
  • When the person is done without signing in, for example on a shared phone, call crm.endVisitor() from a coroutine. The visitor is ended and forgotten, and the next open makes a new one. It does nothing while a user is signed in.
  • With the switch off, the messenger asks the person to sign in, as it always has.

In-app messages, pushes, the inbox of notices and links stay for signed-in users. Anything of yours that reads crm.session as “someone is signed in” should check crm.visitorId too.

With your own sign-in’s key set (advanced)

Section titled “With your own sign-in’s key set (advanced)”

If your sign-in already issues tokens and publishes its signing keys (a JWKS at a public https address, with EdDSA, RS256 or ES256), name its issuer, key set address, audience, kind and subject claim in the Your own sign-in (key set) card, and press Check sign-in. Hand the SDK those tokens through your own CrmTokens:

NorthwindTokens.kt
class NorthwindTokens(private val session: NorthwindSession) : CrmTokens {
override suspend fun current(): String? {
// The token for the user signed in, refreshed when it has run out;
// null for nobody.
return session.freshAccessToken()
}
override suspend fun expired(token: String) {
// PurrCRM refused [token] as expired: forget it, so the next
// current() gets a new one. The request is then asked once more.
session.markExpired(token)
}
}

Several requests may ask at once: get one fresh token for all of them, not one each. If current() cannot answer, throw: the request fails without being sent, as CrmOffline unless CrmAuth.tokens(tokens, tokenFailure = { e -> ... }) says which CrmFailure it is.

An app whose team turned on Unverified sign-in takes the user’s id with no token:

Crm(this, config, CrmAuth.unverified { currentUserId }) // null while nobody is signed in

Paste one of your backend’s tokens into the Test a token card on Settings › your app › Developers › Sign-in. It says which contact the token signs in, or exactly why it is refused: signed with no secret of the app’s, a revoked one, expired, too long, the wrong iss, aud or knd, or no sub.

A token that can never work (the wrong way, too long) is refused with that sentence, which the SDK writes to the log once (Logcat, tag crm), and it does not ask again. A token PurrCRM refuses as expired is forgotten, and the request is asked once more with a new one.

On the phone, the setup check reads the token’s claims (never showing the token) and makes one call as the user, so most of these show as a red row with the reason.

When your team archives or deletes the app, PurrCRM refuses every request from it, and the SDK reads that as CrmAppUnavailable. It is final for the life of that Crm: nothing more is sent, and every later request fails with it at once. Calls that answer null or false on a failure do so. Restoring the app brings it back at the next launch.

Push.