Why identify users
Most products ship on more than one platform: a mobile app, a website, maybe a desktop app. In Lerix they all belong to one project, with one API key. By default every install is anonymous: one person using your iOS app, your Android tablet app and your website counts as three unrelated devices. When your apps tell Lerix who is signed in, those devices are linked to your own user ID, and your backend can send to that ID instead of collecting device tokens:user_123 on every device where they are signed in, on
every platform (iOS, Android, web and macOS), each through its own push
provider.
- One install row per device. Devices are linked to a user, never merged, so history and delivery status stay per device.
- Any number of devices per user. Signing in on a new device adds it.
- Your ID, not ours. Use whatever identifies the user in your own
system: a database ID, a UUID, or an email address (1 to 255 characters).
The install ID returned by
getUserId()is a different, Lerix-generated value and doesn’t change.
Set the user after login
CallsetUser as soon as the user signs in, and clearUser when they sign
out. The ID is stored locally and sent again automatically if the install is
ever re-registered, so you only need to call it once per login. It’s safe to
call before the SDK has finished initializing.
The second argument, identityHash, is only needed when you turn on
identity verification. Leave it out until then.
setUser(externalId, identityHash?) and
clearUser(). The JavaScript SDKs take the hash as an options object
({ identityHash }); the others take it as a named argument.
Identity verification
Your apps hold only the public project key, so without verification any client could callsetUser with someone else’s ID and receive their
notifications. Identity verification closes that gap: your own backend,
which already knows who is signed in, signs the ID with a secret that never
ships in an app, and Lerix rejects any ID that isn’t signed.
It’s off by default so that setUser works out of the box. Turn it on before
you go to production.
1
Generate a secret
In the dashboard, open your project, then
Notifications → Settings → Identity verification, and generate a
secret. It’s shown once: store it in your backend’s secret manager
(for example as
LERIX_IDENTITY_SECRET). Generating a new one later
replaces the old one, so hashes computed with the old secret stop
working.2
Compute the hash on your server
The hash is the lowercase hex HMAC-SHA256 of the user ID, keyed with the
secret exactly as shown (the secret’s text, not decoded bytes). Return it
to your app with the login response, next to the user ID.
3
Pass the hash to setUser
Update your apps to pass the hash as
identityHash. Once a secret exists,
a hash that is sent must be correct even while verification is
optional, so a misconfigured backend shows up early.4
Require verified identity
When every app version in use sends the hash, turn on Require verified
identity in the same settings panel. From then on,
setUser without a
valid hash is rejected.Errors from setUser
Send to a user
From your backend, send withexternalUserIds and a private key in the
lerix-key header (see the API introduction):
201 Created
unknownUserIdslists IDs with no linked device (for example, a user who hasn’t installed the app yet). That isn’t an error.- Don’t combine
externalUserIdswithdeviceTokensorsendByTopicin the same request; that returnsNOTIFICATION_TARGET_INVALID. - If you schedule the send with
sentAt, devices are looked up again at send time, so a device the user signs in on after you schedule still gets it. - If one platform has no push keys yet (say, no APNs key), the other devices
still receive the notification, and that device’s delivery shows
PLATFORM_NOT_CONFIGUREDin delivery status.