Authentication
Sign in storefront customers and operator accounts with email codes
Arky has two separate sign-in surfaces:
initializefromarky-sdk/storefrontkeeps a store’s customer sessions;createAdminfromarky-sdk/adminkeeps an operator account session.
Storefront Customers
import { initialize } from "arky-sdk/storefront";
import { createOrderToken } from "arky-sdk/utils";
const arky = initialize("arky_pk_...", {
locale: "en",
createVisitorIdentity: () => ({
customer_id: crypto.randomUUID(),
session_id: crypto.randomUUID(),
}),
createSessionId: () => crypto.randomUUID(),
createId: () => crypto.randomUUID(),
createOrderToken,
});
A first stateful call (a cart, a form, a chat) starts a visitor session: a customer with a visitor token, kept by the SDK. To sign the customer in, ask for a code and verify it:
const requested = await arky.customer.requestCode({
contact: { email: "[email protected]", language: "en" },
});
console.log(requested.email_verification.expires_at);
await arky.customer.verify({ code: "123456" });
const me = await arky.customer.getMe();
if (me.customer.type.type === "verified") console.log(me.customer.type.contact.email);
- The code email’s id comes from your
createId. When the answer is lost, requesting the same contact again reuses that id, so the customer gets one email; passidyourself to pick it. - The
contactnames the email and the language of the code email, one of the store’s languages (409 CUSTOMER.LANGUAGE_NOT_SUPPORTEDotherwise). Arky never picks a language or an address for you. - The code email goes out with the store’s
customer_sign_inselection. A code request is refused at once while the store has none selected or its email sending is paused (409 CUSTOMER_SIGN_IN.UNAVAILABLE) or its sending address can’t send (409 EMAIL_ADDRESS.NOT_SENDING), so show your own message then. See Store Email. - A code lasts ten minutes and allows five wrong tries; an address gets at most one new code a
minute. Each refusal has its own
409code, so show your own text for it:CUSTOMER_SIGN_IN.CODE_RECENT(a new code too soon),CUSTOMER_SIGN_IN.CODE_INVALID(a wrong or expired code, or none waiting; it counts as a wrong try) andCUSTOMER_SIGN_IN.ATTEMPTS_EXHAUSTED(too many wrong codes for the address). - When the publishable key has a bot check, send the provider’s token as
bot_check_token. A missing token is refused with409 BOT_CHECK.REQUIREDand one the provider rejects with409 BOT_CHECK.REFUSED;503 BOT_CHECK.UNAVAILABLEmeans the provider couldn’t be reached, so the same request can be sent again with a fresh token. verifymakes the signed-in session under an id from yourcreateSessionId, and the customer becomesverifiedwith that contact. If another customer of the store already holds that address, the visitor’s cart and history move to that customer and the session continues as them.
Change The Email
On a signed-in session, requestCode starts an email change instead of a sign-in. The code goes to
the new address, and changeEmail proves it:
await arky.customer.requestCode({
contact: { email: "[email protected]", language: "en" },
});
await arky.customer.changeEmail({ code: "654321" });
The code goes out as the store’s customer_email_verification email. The session stays signed in
and the new contact becomes the verified one. The customer’s other signed-in sessions end, and the
previous contact gets the store’s email_changed email in its own language. A change to an address
another verified customer holds is refused (409 CUSTOMER.EMAIL_TAKEN), and so is a change to
the customer’s current address (409 CUSTOMER.EMAIL_UNCHANGED) and the code request while the store has no
customer_email_verification or email_changed email selected. A token refresh in between
keeps the pending change, so the code still completes it.
updateMe can’t replace or remove a proven email; only a code changes it. To sign in as someone
else instead, call logout first.
Read the session, follow changes, or sign out:
console.log(arky.session.get(), arky.isAuthenticated);
const stop = arky.onAuthStateChanged((session) => {
console.log(session?.customer.id ?? "signed out");
});
await arky.customer.logout();
stop();The arky_pk_... key identifies the store and is safe in browser code. It signs nobody in.
Never put an account token in storefront code; the SDK creates and stores the customer’s own session
tokens.
Operator Accounts
import { createAdmin } from "arky-sdk/admin";
const sdk = createAdmin({ apiUrl: "https://api.arky.io" });
const pending = await sdk.account.auth.requestCode({ session_id: crypto.randomUUID(), email: "[email protected]" });
await sdk.account.auth.verify({ session_id: pending.session_id, code: "123456" });
const account = await sdk.account.getMe();
The Arky sign-in code is one of Arky’s own emails, sent from Arky’s address. requestCode takes
the pending session’s session_id, a UUID v4 your code picks once per “send code” press and sends
again on a retry, so a lost answer never sends a second code. For a store’s own login page, pass
the store’s id to both calls, and the code email’s language to the request:
sdk.account.auth.requestCode({ session_id, email, store_id, language }) and
sdk.account.auth.verify({ session_id, code, store_id }). That code is the store’s own
store_admin_sign_in email, from the store’s sender, and the session it opens works only in that
store. The Admin client keeps and refreshes its session.
For trusted jobs, make an account token and pass it as accountToken; see
Account Tokens.
Expired Codes
A customer code is accepted only by the session that asked for it, and an account code only with
its own session_id. When a code expires, ask for a new one instead of retrying old state.
import { ArkyApiError } from "arky-sdk/storefront";
try {
await arky.customer.verify({ code: "000000" });
} catch (error) {
if (error instanceof ArkyApiError && error.code.startsWith("CUSTOMER_SIGN_IN.")) showCmsText(error.code);
else throw error;
}