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
- Turn on the email link method. Send a
PATCHrequest to/v1/settingswith{"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.
Ask for a link
-
Send the request. From the customer's page or app, send a
POSTrequest to/v1/client/auth/linkwith the publishable key inBlack-Publishable-Keyand the customer'semailandredirect_url. A page sends noAuthorizationheader.redirect_urlis the address of your page that receives the link. See Email a customer a link that signs them in. -
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.
Read the link on your page
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.
- Read the token. The page at
redirect_urlreadsblack_linkfrom the fragment of its own address, then removes the fragment from the address bar withhistory.replaceState.
Exchange the link for a session
-
Send the token to Black. Send a
POSTrequest to/v1/client/auth/link/exchangewith the publishable key and thetoken. See Exchange a sign-in link for a customer session. -
Read the session. The response is
201and holds the customer's session: itsid, itscustomer, itsapp,access_tokenandaccess_token_expires_at,refresh_tokenandexpires_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.
- Exchange the code. Send a
POSTrequest to/v1/client/auth/link/exchangewith the publishable key, the customer'semailand thecode, in place of thetoken. No session is needed, and the response is the same201and 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.
403sign_in_method_disabled. The email link method is off. Turn onsign_in.email_link, then send the call again.401link_used. The link was already exchanged. Each link works once. Ask for a new link.401link_expired. The link is older than 15 minutes. Ask for a new link.422link_invalid. The token is malformed, unknown, of another kind of link or of another environment. The error'sparamnamestoken. Ask for a new link.409blocked. The customer is blocked. No session is made, and the link stays unused. Unblock the customer first: see Block a customer.422parameter_invalid. A field is wrong. The error'sparamnames it: theemail, or theredirect_url, which must be on one of the app's allowed origins.429rate_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 secondsRetry-Aftergives, then send the call again.403origin_not_allowedand401publishable_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
- Read each call's fields and answers in Email a customer a link that signs them in and Exchange a sign-in link for a customer session.
- Read how a customer signs in with a password in Sign customers in with email and password.
- Read how a customer you already sign in elsewhere gets a session in Sign customers in with your own sign-in.
- Read how to change settings in Change account settings.
- Read every error code in Errors.