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 the Identity secret card, press Make a secret (crm_is_…). It is shown once. Keep it in your backend’s secrets, never in the page.
  2. Your backend signs a short token for the signed-in user with it.
  3. The page asks your backend for the token, and the SDK sends it.

Sign an HS256 token with the user’s id as sub, an iat, and an exp minutes or hours away (more than 30 days is refused). 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 it from a route of your own, behind your own sign-in, such as /crm-token.

Sign users in under Web app and backend has the code in Node, Python, Go, PHP and Ruby, and a route that serves it. The page asks for the token on your own site, so your usual session cookie signs that request in; the token itself travels to PurrCRM in Authorization, never in a cookie.

The user’s id must be the one your backend uses for them in Identify, so that what the page 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:

src/crm.ts
import { Crm, CrmAuth, CrmTokens } from "crm-sdk";
export const tokens = CrmTokens.fetched(async () => {
const answer = await fetch("/crm-token"); // signed by your backend
if (answer.status === 401) return null; // nobody is signed in
if (!answer.ok) throw new Error(`token: ${answer.status}`);
return (await answer.json()).token;
});
export const crm = new Crm({ config, auth: CrmAuth.tokens(tokens), host });

Your function answers the token as a string, null when nobody is signed in, or a new CrmFetchedToken(token, expiresAt) when your backend says when it expires. If it throws, the request fails without being sent, as CrmOffline; CrmAuth.tokens(tokens, { tokenFailure: (error) => ... }) names another CrmFailure for it.

CrmFetchedTokens:

  • keeps the token in memory only, never in localStorage, sessionStorage or a cookie;
  • keeps it until a minute before its exp (halfway to it, for a token that lasts less than two minutes);
  • fetches once for every request waiting on a token, rather than once each;
  • fetches a new one when PurrCRM refuses the old one as expired, and asks the request again once;
  • times the token by its own exp less iat when the browser’s clock is wrong, rather than fetching for every request. That is why you should sign an iat.

When the user signs out, call tokens.forget(): it drops the token, and one on its way.

await crm.forgetPushToken(); // while their token still works: see Push
tokens.forget();
crm.session(null);

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.

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

src/tokens.ts
import type { CrmTokens } from "crm-sdk";
export const northwindTokens: CrmTokens = {
async current() {
// The token for the user signed in, refreshed when it has run out;
// null for nobody. Several requests may ask at once: get one fresh
// token for all of them, not one each.
return await mySignIn.accessToken();
},
async expired(token) {
// PurrCRM refused `token` as expired: forget it, so the next
// current() answers a new one. The request is then asked once more.
mySignIn.dropToken(token);
},
};

If current() throws, the request fails without being sent, as for CrmTokens.fetched.

An app whose team turned on the Unverified sign-in card (it asks them to type the app’s name) takes the user’s id with no token:

new Crm({ config, auth: CrmAuth.unverified(() => currentUserId ?? null), host });

The SDK sends the id as X-CRM-User-Id. The function answers 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 whose signature or time is wrong is refused as expired (token_expired), and the SDK asks your backend for a new one, once. A token that can never work (the other mode’s, too long, no sub) is refused for good: the SDK writes PurrCRM‘s sentence saying why to the logger, once, and does not ask again. The request fails as a CrmProblem with status 401, and your screens get a CrmMessage whose sentence is signInAgain.

With debug on, the setup check reads the token’s claims for you (sub against the session’s user, exp, iat), and makes one call as the user to show whether PurrCRM takes it (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, and every call fails at once with the same refusal, so nothing waits on a network. Restoring the app brings it back at the next page load.

Push.