Identity verification
Without identity verification, anyone who knows a user’s ID can call identify with it from their own browser and see that user’s content and history. Identity verification closes that gap. Your server signs each user ID with a secret only you and UserGuiding know, and UserGuiding rejects any identify call whose signature does not match.
How it works
Section titled “How it works”- You turn on identity verification in the panel and receive a secret key.
- For each signed in user, your server computes
user_hash, an HMAC SHA256 of the user ID keyed with the secret, encoded as hex. - Your frontend passes
user_hashtoidentifyalong with the user ID. - UserGuiding recomputes the hash and only accepts the request when the two match.
Set it up
Section titled “Set it up”Get your secret
In the panel, go to Installation > User Identification and click Enable Identity Verification. Copy the secret key it shows.
Generate the hash on your server
Compute the hash from the exact user ID string you pass to identify. If your IDs are numbers, hash their string form, for example "42".
import crypto from 'node:crypto'
const userHash = crypto .createHmac('sha256', process.env.USERGUIDING_IDENTITY_SECRET) .update(String(user.id)) .digest('hex')import hashlibimport hmacimport os
user_hash = hmac.new( os.environ["USERGUIDING_IDENTITY_SECRET"].encode(), str(user.id).encode(), hashlib.sha256,).hexdigest()require 'openssl'
user_hash = OpenSSL::HMAC.hexdigest( 'SHA256', ENV.fetch('USERGUIDING_IDENTITY_SECRET'), user.id.to_s)$userHash = hash_hmac( 'sha256', (string) $user->id, getenv('USERGUIDING_IDENTITY_SECRET'));import ( "crypto/hmac" "crypto/sha256" "encoding/hex" "os")
mac := hmac.New(sha256.New, []byte(os.Getenv("USERGUIDING_IDENTITY_SECRET")))mac.Write([]byte(userID))userHash := hex.EncodeToString(mac.Sum(nil))import java.nio.charset.StandardCharsets;import java.util.HexFormat;import javax.crypto.Mac;import javax.crypto.spec.SecretKeySpec;
Mac mac = Mac.getInstance("HmacSHA256");mac.init(new SecretKeySpec( System.getenv("USERGUIDING_IDENTITY_SECRET").getBytes(StandardCharsets.UTF_8), "HmacSHA256"));String userHash = HexFormat.of().formatHex( mac.doFinal(userId.getBytes(StandardCharsets.UTF_8)));Generate the hash on each page load or sign in and send it to the frontend. Do not store it in your database. If you rotate the secret, stored hashes all break at once.
Pass the hash to identify
window.userGuiding.identify(user.id, { email: user.email, user_hash: userHashFromServer,})Check the installation
Back in the panel, click Check Installation. Once the check passes, identity verification is enforced for your project.
After it is enforced
Section titled “After it is enforced”- An
identifycall without auser_hashis refused, and the SDK logsuser_hash is required when identity verification enabledto the console. - A call with the wrong hash is rejected by the server, so no content loads for that user.
- Hex digests are compared case insensitively, so uppercase output (for example from .NET’s
BitConverter) also works. - Call
clearIdentityon sign out to remove the stored hash from the browser.