Skip to main content

Sign customers in with an email link

What you will build​

You let a customer sign in to your app with a link that Black emails them, with no password to keep. The customer enters an email, opens the link in the mail, and your page exchanges the link's token for a customer session: an access token and a refresh token that the page or app uses for the calls of the Client API.

You turn the method on, build the request for a link and the exchange of the link, read each refusal, and learn what the sign-in does to the customer's account. The method makes a customer who has none: a person who opens a link for an email that no customer holds becomes a new customer, with that email proved.

Before you start​

You need an app with a publishable key, in the environment where you sign customers in. Create the app with Register an app and its key with Create a publishable key. A publishable key is an identifier, not a secret: your page or app sends it in the Black-Publishable-Key header. A web app is called from a page whose origin is one of its allowed origins.

The page that receives the link must be on one of the app's allowed origins, because Black puts the link's token in the address of that page.

The settings are changed by the account's own API client, or by its vendor with Black-Account, as every other setting is: see Change account settings.

Steps​

Turn the method on​

  1. Turn on the email link method. Send a PATCH request to /v1/settings with {"sign_in":{"email_link":true}}. The response holds all the settings as they are after the change. See Change an environment's settings. The method is off in a new environment, and it is independent of the password method.

While the method is off, both calls of this guide answer 403 with sign_in_method_disabled, before Black reads the request. Turning the method off does not end the sessions it already made.

  1. Send the request. From the customer's page or app, send a POST request to /v1/client/auth/link with the publishable key in Black-Publishable-Key and the customer's email and redirect_url. A page sends no Authorization header. redirect_url is the address of your page that receives the link. See Email a customer a link that signs them in.

  2. Read the answer. The response is always 202, with the same body and after the same wait, whether or not the email belongs to a customer, so the answer does not show who has an account. Say "If the email can sign in, a link is on its way" and nothing more.

Black emails a link to any email, including one that no customer holds yet, except the email of a customer who is blocked. Two limits apply to this call. Black takes three requests for one email in 10 minutes, counting the email in lower case and without spaces around it, in each environment apart. The fourth answers 429 with rate_limited and a Retry-After header that holds the seconds to wait, sends nothing, and answers the same for an email that is a customer's and one that is not. A sign-in does not reset the count: the customer waits for the seconds Retry-After gives. Black also counts this call among the sign-ins of one address and key, 30 a minute.

The link in the mail is your redirect_url with the token in its fragment, #black_link=<token>, so neither your servers nor any log between them sees the token. The redirect_url must be on one of the app's allowed origins, so no link leads to another site. The link works once and for 15 minutes.

  1. Read the token. The page at redirect_url reads black_link from the fragment of its own address, then removes the fragment from the address bar with history.replaceState.
  1. Send the token to Black. Send a POST request to /v1/client/auth/link/exchange with the publishable key and the token. See Exchange a sign-in link for a customer session.

  2. Read the session. The response is 201 and holds the customer's session: its id, its customer, its app, access_token and access_token_expires_at, refresh_token and expires_at. Both tokens are shown once: keep them where the customer's page or app keeps its session, and never in a log. The access token lasts an hour. Renew the pair before the hour ends with Refresh a customer session.

A link that is used twice makes one session: the second exchange is refused.

Sign a mobile app in​

A mobile app (kind ios or android) has no allowed origins, and takes the link in one of two ways.

An app registered with return_urls sends a redirect_url that is exactly equal to one of them: no longer path, no shorter path, no query. Any other address answers 422 with parameter_invalid, naming redirect_url, and nothing is sent. The mail holds that address with #black_link=<token>, and the app opens it from its universal link (iOS) or app link (Android) and exchanges the token as above. Test this on a device, iOS and Android, before you ship: check that your app receives the whole link, fragment included, when the customer opens it from the mail. If the app is not installed, the link opens the address in the browser.

An app registered without return_urls sends no redirect_url: one sent by such an app answers 422 with parameter_invalid. The mail holds an 8-digit code, shown as two groups of four such as 1234 5678, in place of a link. The code works once and for 15 minutes, and a new request replaces the earlier code.

  1. Exchange the code. Send a POST request to /v1/client/auth/link/exchange with the publishable key, the customer's email and the code, in place of the token. No session is needed, and the response is the same 201 and session as for a link. A space between the two groups is accepted.

Every failure of a code answers 422 with link_invalid, whether the code is wrong, used, expired, replaced or tried too often, or the email is no customer's. Five wrong tries end the live code, and the customer asks for a new one. Ten exchanges with a code in an hour for one email give 429 with rate_limited and a Retry-After header.

Know what the sign-in does to an account​

The link proves that the person owns the inbox, so the sign-in counts the customer's email as proved. How it finds the customer depends on the email:

  • A customer who already signs in with that email is signed in.
  • A customer who holds the email proved on another way of signing in gets an email identity added, and is signed in.
  • An email that no customer holds makes a new customer with that email, proved from the start.

A password may have been set by someone other than the owner of the inbox, because a sign-up can name an email that belongs to someone else. When the link signs in a customer whose password was not proved by the owner, Black secures the account. Every session of the customer ends, on every device, with the end reason account_secured, and each ended session records customer_session.ended. The password is removed, and Black emails the owner of the email that their account was secured. The customer sets a new password with a reset: see Sign customers in with email and password. A proved password stays, and no session ends.

Handle each refusal​

Each refusal is an error with a code: see Errors for every code.

  • 403 sign_in_method_disabled. The email link method is off. Turn on sign_in.email_link, then send the call again.
  • 401 link_used. The link was already exchanged. Each link works once. Ask for a new link.
  • 401 link_expired. The link is older than 15 minutes. Ask for a new link.
  • 422 link_invalid. The token is malformed, unknown, of another kind of link or of another environment. The error's param names token. Ask for a new link.
  • 409 blocked. The customer is blocked. No session is made, and the link stays unused. Unblock the customer first: see Block a customer.
  • 422 parameter_invalid. A field is wrong. The error's param names it: the email, or the redirect_url, which must be on one of the app's allowed origins.
  • 429 rate_limited. One of the limits of the call is reached: a fourth request for one email in 10 minutes, or more than 30 sign-in calls a minute from one address and key. Wait for the seconds Retry-After gives, then send the call again.
  • 403 origin_not_allowed and 401 publishable_key_invalid. The request does not come from one of the app's allowed origins, or the key is missing, unknown or revoked. Send the call from the app's page with the app's current key.

A runnable sample​

The link arrives by email, so a sample cannot read its token, and this sample does not complete a sign-in. It registers an app and its publishable key, turns the method on, asks for a link, which answers 202, and sends the exchange a made-up token, which is refused with 422 and link_invalid. 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, and each write waits a second because a test environment takes five writes a second.

TOKEN=PLACEHOLDER_ACCESS_TOKEN
EMAIL="link-$(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":"Link Demo","kind":"web","allowed_origins":["https://app.example.com"]}' \
| sed -n 's/.*"id":"\(app_[0-9a-z]*\)".*/\1/p')

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')

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

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

curl --silent --request POST https://api.black.example/v1/client/auth/link/exchange \
--header "Black-Publishable-Key: $KEY" \
--header "Origin: https://app.example.com" \
--header "Content-Type: application/json" \
--data '{"token":"not-a-real-link-token"}'

The app and its key answer 201. The settings change answers 200 and holds sign_in.email_link as true. The request for a link answers 202. The exchange of the made-up token answers 422 with link_invalid, naming token.

What to do next​