Skip to content

Sign users in

Your app signs its users in; the SDK never does. 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.

  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.

CrmFetchedTokens asks your backend for the token whenever it needs one:

AppCrm.swift
let crmTokens = CrmFetchedTokens {
// Your backend signs a token for the user signed in; nil for nobody.
guard let token = try await MyBackend.shared.get("/crm-token") else { return nil }
return CrmFetchedToken(token)
}
@MainActor
let crm = Crm(config: config, auth: .tokens(crmTokens))

It keeps the token until a minute before its exp (halfway through the life of a shorter one), fetches once for every request waiting on one, and fetches a new one when PurrCRM says it has expired. A phone whose clock is wrong does not make it fetch 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: date).

The token is kept in memory only, never on the phone’s storage. If your fetch throws, the request fails without being sent, as CrmOffline unless CrmAuth.tokens(tokens, tokenFailure:) turns your error into another CrmFailure.

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:

await crm.forgetPushToken() // while their token still works, see Push
await crmTokens.forget()
crm.setReady(false)
crm.session(nil)
  • guestId is the user’s id in your app (the token’s sub). displayName is how your team sees them.
  • Until setReady(true), no in-app message is handed to you (crm.inApp.next and crm.inApp.banner stay nil, and what came waits), and a tapped push or an opened link waits. Call it again whenever that changes.
  • CrmSession(guestId:productEventsOff: true) records no product events for a user who turned them off in your settings.
  • CrmSession(guestId:features:) holds parts back from some users (Messenger).

A new user signing in reads their messenger and inbox, registers the push token for them, and checks in for messages. Signing out lets go of everything the SDK showed for the last user.

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 on the Your own sign-in (key set) card, and press Check sign-in. Hand the SDK those tokens through your own CrmTokens:

NorthwindTokens.swift
actor NorthwindTokens: CrmTokens {
func current() async throws -> String? {
// The token for the user signed in, refreshed when it has run out;
// nil for nobody.
}
func expired(_ token: String) async {
// PurrCRM refused `token` as expired: forget it, so the next
// current() gets a new one. The request is then asked once more.
}
}
let crm = Crm(config: config, auth: .tokens(NorthwindTokens()))

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.

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

let crm = Crm(config: config, auth: .unverified { currentUserId })

The closure answers the signed-in user’s id, or nil; the SDK sends it as X-CRM-User-Id.

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 refused as expired is forgotten, and the SDK asks your backend for a new one and sends the request again, once. Any other refusal fails the request, and the SDK writes PurrCRM‘s sentence to the log once a launch (CrmSdk: the CRM refused the guest's token: …). The setup check’s Token row reads your token as a JWT and names what is missing (The setup check).

When your team archives or deletes the app, PurrCRM refuses every request from it, and the SDK reads that as CrmAppUnavailable (a CrmProblem). It is final for the life of that Crm: nothing more is sent, every call fails with it, and a conversation on screen is marked gone. Restoring the app brings it back at the next launch.

Push.