Skip to main content

Know how Black matches a customer

What you will build​

You learn which customer Black signs in when a person uses Black's own sign-in, so your pages and your records agree about who is who. At the end you can predict whether a sign-in finds a customer, links to one or makes a new one, and you can react when Black cannot tell.

These rules apply to Black's own sign-in: email and password, an email link and Google. A customer signed in by your own sign-in token or identity token is found by your own reference instead: see Sign customers in with your own sign-in.

Before you start​

You need an app with a publishable key and at least one method of Black's sign-in turned on: see Choose how customers sign in.

An identity is one way of signing in that belongs to a customer: an email, with its password if it has one, or a Google account. An email identity is proved once its customer shows they own the inbox, by the verification link, by completing a password reset or by signing in with an email link. Google proves the email of its token when Google says it is verified and the address is a Gmail address or the token names a hosted domain.

Steps​

Follow the matching rules​

Black applies these rules in order, in one step per email, so two sign-ins at once make one customer.

  1. A sign-in whose identity Black already holds signs that customer in. The identity is the email in lower case, or Google's own account ID, never the email Google reports.

  2. Otherwise, a sign-in that arrives with a proved email links to the one customer of the environment who holds that email proved on an identity. The new way of signing in is added to that customer and they are signed in. Black records customer_identity.linked. The customer is the same one however they sign in: a person who signed up with a password and later uses an email link is one customer.

  3. Otherwise, Black makes a new customer, with the proved email when the sign-in has one.

  1. Expect no link for an email that is not proved. A customer whose email is not proved never gets an identity linked by that email. An email anyone can type into a sign-up form proves nothing about the inbox, so a sign-up with a password never links to an existing customer. It is refused with 409 and email_in_use when an email identity of the environment already holds the email, or when a customer holds it proved on any identity. Anything else makes a new customer, so a customer your account made with that email stays apart.

  2. Expect a secured account when a link proves an email that a password was set on without proof. A person may sign up with an email that is not theirs. When the real owner then signs in with a link for that email, Black removes the password nobody proved, ends every session of the customer with the end reason account_secured, and emails the owner of the email that their account was secured. The owner sets a password again with a reset. A password the owner proved stays, and no session ends.

Read customer_identity.ambiguous​

  1. Watch for the event. When two or more customers each hold the email proved, Black cannot tell which one the person is. It links to none of them, makes a new customer, and records customer_identity.ambiguous. The event lists the customers who hold the email in data.ambiguity.customers. See Events for the table.

  2. Decide who is who. Read each listed customer, merge what you want to keep by your own means, and remove the identity from the one that should not hold it. Send a GET request to /v1/customers/{id}/identities to list a customer's identities: see List a customer's sign-in identities. Send a DELETE request to remove one: see Remove a customer's sign-in identity. A removed identity no longer signs the customer in, and the sessions it made live on.

A customer who is deleted never matches, and an identity a deleted customer left signs no one in.

A runnable sample​

This sample shows two of the rules with the password method. It makes a customer on your server with an email, then signs up with that email: the customer you made holds it unproved, so the sign-up makes a new customer and answers 201. A second sign-up with the same email answers 409 with email_in_use. It writes PLACEHOLDER_ACCESS_TOKEN where your access token goes, reads the app's ID and the key from the responses, and needs sed. The email is made new on each run, the password is for the sample only, and each write waits a second because a test environment takes five writes a second.

TOKEN=PLACEHOLDER_ACCESS_TOKEN
EMAIL="match-$(date +%s)@example.com"

APP=$(curl --silent --request POST https://api.black.example/v1/apps \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"name":"Match Demo","kind":"web","allowed_origins":["https://app.example.com"]}' \
| sed -n 's/.*"id":"\(app_[0-9a-z]*\)".*/\1/p')
sleep 1

KEY=$(curl --silent --request POST "https://api.black.example/v1/apps/$APP/publishable-keys" \
--header "Authorization: Bearer $TOKEN" \
| sed -n 's/.*"\(pk_test_[0-9A-Za-z]*\)".*/\1/p')
sleep 1

curl --silent --request PATCH https://api.black.example/v1/settings \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"sign_in":{"password":true}}'
sleep 1

curl --silent --request POST https://api.black.example/v1/customers \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data "{\"name\":\"Made by the account\",\"email\":\"$EMAIL\"}"
sleep 1

curl --silent --request POST https://api.black.example/v1/client/auth/sign-up \
--header "Black-Publishable-Key: $KEY" \
--header "Origin: https://app.example.com" \
--header "Content-Type: application/json" \
--data "{\"email\":\"$EMAIL\",\"password\":\"sample-password-one\",\"redirect_url\":\"https://app.example.com/verify\"}"

curl --silent --request POST https://api.black.example/v1/client/auth/sign-up \
--header "Black-Publishable-Key: $KEY" \
--header "Origin: https://app.example.com" \
--header "Content-Type: application/json" \
--data "{\"email\":\"$EMAIL\",\"password\":\"sample-password-two\",\"redirect_url\":\"https://app.example.com/verify\"}"

curl --silent --request PATCH https://api.black.example/v1/settings \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/json" \
--data '{"sign_in":{"password":false}}'

The app, its key and the customer you made answer 201. The settings change that turns the method on answers 200. The first sign-up answers 201 and holds a new customer's session. The second sign-up answers 409 with email_in_use, because an email identity now holds the email. The last change answers 200 and switches the method off.

What to do next​