Members
Let your customers sign in on your site with an emailed code, read the posts you keep for members or buyers, and see their orders, with bf.members.
bf.members is the SDK surface of the Members plugin. Once Members
is on for a workspace (Settings → Plugins → Members), your customers sign
in on your site with a 6-digit code sent to their email, and with Google
or a password when the business switches those on.
Each member is in People, with their chats, bookings and orders.
How the session works
Every call goes to your site's own address, /_bf/members/*. On a
site Brandfine hosts on its own domain, Brandfine's edge forwards that
path to Brandfine, so the sign-in cookie belongs to your domain: it is
HttpOnly (no script can read it), Secure and SameSite=Lax, and it
lasts 30 days while it is used. A cookie set by api.brandfine.co would
be a third-party cookie, which Safari and Firefox block.
- Browser only.
bf.members.*throws outside a browser. - Under
astro dev, the starter of sites Brandfine builds forwards/_bfto the API in itsastro.config.mjs, and the cookie is set onlocalhost. - Draft previews and sites without their own domain have no sign-in: each is one address shared by many sites.
Sign in
import { createBrandfineClient, MembersError } from '@brandfine/client'
const bf = createBrandfineClient({ apiKey: import.meta.env.PUBLIC_BRANDFINE_KEY })
const session = await bf.members.session()
// { enabled: true, signup: 'open', member: null, methods: ['email'] }
// Step 1: the visitor types their email. Always { sent: true }: the answer
// never tells whether the email is a member. `returnPath` is where the
// emailed link lands; `locale` picks the email's language.
await bf.members.requestCode({ email, returnPath: location.pathname, locale: 'tr' })
// Step 2: they type the 6-digit code from the email.
try {
const { member } = await bf.members.verifyCode({ email, code })
} catch (err) {
if (err instanceof MembersError && err.code === 'members.code_invalid') {
// wrong, used or expired: ask for a new one
}
}
await bf.members.signOut()The email also carries a link (/_bf/members/link?t=…) that signs in and
returns to returnPath; an expired or used link lands there with
?signin=expired. Codes last 10 minutes and allow 5 tries. A visitor can
ask for 5 codes an hour per email (a 429 with external.rate_limited).
Who may sign up is a setting: anyone with an email, or only people you add (Members → Members → Add a member). On the second, a stranger gets an email saying there is no account, and no code.
Sign in with Google
When the business connects its own Google app (Integrations → Google
sign-in) and switches Google on (Members → Settings → Sign-in methods),
session().methods carries 'google', only on the site's own domain. The
consent screen is the business's: its name, never Brandfine's.
if (session.enabled && session.methods?.includes('google')) {
// Sends the visitor to the business's Google consent screen. Google brings
// them back to `returnPath`, signed in.
await bf.members.signInWithGoogle({ returnPath: location.pathname })
}A sign-in that didn't work returns to the page with ?signin= and one of
google-cancelled, google-not-allowed (a new email while only people you
add may sign up), google-failed or expired. Show a line for it and
offer the email code, which always stays on. The member is found by the
email Google confirmed, so someone who used the code before is the same
member, with the same purchases. startGoogleSignIn() returns the address
without going there.
Sign in with a password
When the business switches Email and password on (Members → Settings →
Sign-in methods), session().methods carries 'password', and the
signed-in member's hasPassword says whether they chose one.
await bf.members.signInWithPassword({ email, password })A wrong pair, an unknown email and a member with no password all get the
same members.password_wrong, so the form never tells who is a member.
After too many wrong tries for an email, members.password_paused (429)
holds it for an hour; the email code still works.
A password is set only by a signed-in member. "Create an account" and "Forgot your password?" are the email code, then the new password:
await bf.members.requestCode({ email })
await bf.members.verifyCode({ email, code })
await bf.members.setPassword({ password, locale: 'tr' })So nobody can set a password on someone else's email. A password Brandfine
can't use is a 400 members.password_weak with params.reason: short
(under 8 characters), long (over 128), email, or breached (found in a
known data breach). Changing a password more than 15 minutes after signing
in needs currentPassword (members.password_current_needed otherwise).
Every other browser signed in as the member is signed out, and they get an
email saying their password was set or changed.
Protected posts
A post's access is set in its editor (or on its post type):
PUBLIC, MEMBERS (anyone signed in) or BUYERS (a member who bought a
product that unlocks it). The public posts API serves a protected post as
its teaser only, from every key: title, description, image, category and
dates, with contentHtml: '' and contentMarkdown, customConfig and
jsonLd null. So a static build never holds the body.
// At build time: the teaser, and a place for the body.
const post = await bf.posts.getBySlug(slug)
if (post?.access && post.access !== 'PUBLIC') {
// render the title and description, then load the body in the browser
}
// In the browser:
const result = await bf.members.post(post.id)
if (result.allowed) {
container.innerHTML = result.contentHtml // sanitised by Brandfine when saved
} else if (result.reason === 'sign-in') {
// show the sign-in form
} else {
// result.reason === 'buy': result.products are on sale and open it
}A purchase counts once it is paid, or confirmed or completed by the team, and not cancelled or refunded. It is matched by the email on the order.
Orders
const { items } = await bf.members.orders() // 401 when signed out
// [{ number, token, status, payment, currency, total, createdAt, lines }]
// Open one on your order page: /order/?t=<token>The member sees the orders whose buyer email is the email they signed in with.
What their purchases opened
const { items } = await bf.members.unlocked({ locale: 'tr' }) // 401 when signed out
// [{ id, title, slug, locale, type, description, image }]
// Link each one to its page on your site (type + slug)The posts kept for buyers that a product of their counted orders opens (the post itself, its post type or its category), newest first, one per post in the language you ask for when it has it. Titles and descriptions only: the body still loads on the post's page.
What they bought
const { items } = await bf.members.purchases({ locale: 'tr' }) // 401 when signed out
// [{
// product: { id, slug, name, image },
// page, // the product's page: { slug, locale, type } or null
// active, // paid or confirmed: in effect. false while the order waits
// order: { number, token, status, payment, createdAt },
// variants, // ['20 LT', …]
// delivery, // { city } when it ships, else null
// opens: { posts: [{ id, title, slug, locale, type }], types: [{ slug, name }], categories: [{ slug, name }], pages: [{ path, title, lang }] },
// }]There is one item per product they ordered, newest order first.
Cancelled and refunded orders are left out, and a product archived since
stays. order is the newest order with it, so link it to your order page
with token.
Each item states facts, not a kind of product, so show the parts a purchase carries:
activefalse: "waiting for confirmation";delivery: where it goes;opens: links to what it opens, which are posts, post types and categories kept for buyers, and the site's private pages for buyers (pages, by address, below).
The same markup then fits a shop that ships, a coach selling a guide and a clinic selling sessions.
The order form
const details = await bf.members.checkoutDetails() // 401 when signed out
// { name, email, phone, company, shipping: { address, city, postalCode } | null }Starting values for your order form: the name and email they signed in with, and the phone, company and delivery address of their latest order. Fill only the fields the visitor left empty. Invoice details (ID or tax numbers) are never sent back.
Sites Brandfine builds
Their starter has it ready: <SignIn />, <Account /> (the account page
at /account/, written when Members is on: the member, their orders and
what their purchases opened), <MembersOnly /> (a
protected post's body, used by the blog and post type pages) and
<AccountButton /> in the header, switched on by capabilities.members
in src/lib/site-meta.ts.
Private pages
A site Brandfine builds can also keep a whole page of its own design for members or for buyers. The page carries its rule in its layout:
<SiteLayout title="Lessons" access="members">
<SiteLayout title="Guide" access="buyers" products="garden-kit" locked="/garden-kit/">On the live site the page is in no public storage and no sitemap. Before serving it, the hosting asks Brandfine about the visitor:
- Not signed in: sent to the account page, and back to the page once signed in.
- A member, for a
memberspage: sees it. - A member with a purchase in effect, for a
buyerspage: sees it. Withproducts, it has to be one of those products. - A member without the purchase: sent to
locked, usually the page that sells it, else the account page.
Each rule follows its plugin. A private page needs Members on, and a
buyers page the Shop too. While either is off the page answers "not
found". Images, videos and files linked from the page stay public files.
Private pages work on sites Brandfine hosts, on their own domain; a site
outside Brandfine keeps protected content in posts.
In Brandfine you see them, but don't change them there: the workspace's
Pages tab marks each private page and has a "Private" filter, with who
can open it and where everyone else is sent. A product's Unlocks lists the
private pages it opens. purchases() gives them to the member as
opens.pages, in the language asked for, and gives none while the Shop is
off.