Link users and more
Optional. For apps that already have their own users, that change their UI based on roles, or that read Block-Auth data from the server.
Link existing users (optional)
If your app already has its own users, they can link their Block-Auth identity to their account. After that, signing in with Block-Auth returns their id in your app as externalId, so you can sign them in as your user.
In your frontend, show LinkBlockAuth to a user who is signed in with your own login:
import { LinkBlockAuth } from '@block-auth.io/react'
const messages = {
already_linked: 'This Block-Auth account is already linked to another user.',
not_signed_in: 'Sign in again to link your account.',
failed: 'Linking failed, try again.',
}
export function LinkAccount() {
return (
<LinkBlockAuth
onLinked={() => window.location.reload()}
onError={(reason) => alert(messages[reason])}
/>
)
}
The button opens the login in a popup. If the browser blocks it, it opens in the same tab: Block-Auth sends the user back to the root of your app (/) and LinkBlockAuth finishes the link on the page where it started.
How you add the backend login routes
Applies from here on. You pick it once in the SDK Quickstart.
In your backend, tell the Block-Auth package who is signed in with your own login (appUser) and save the link (onLinked). With appUser, the package turns on GET /link (is this user linked?) and POST /link (links them, or answers 409 if the identity or your user is already linked):
export const routes = new BlockAuthRoutes({
apiKey: BLOCKAUTH_API_KEY,
apiSecret: BLOCKAUTH_API_SECRET,
appUrl: APP_URL,
appUser: async ({ session }) => {
const id = session.get('userId')
return id ? { id, linked: await isLinked(id) } : null
},
onLinked: (id, user) => saveLink(id, user.address),
})
isLinked, saveLink are yours: replace them with your app’s own functions (your database, your cache…).
isLinked and saveLink are yours: they read and write the link in your database. From then on, every Block-Auth login of that user brings your user ID as externalId in the session user: use it to sign them in as your user.
Ask for a recent login before linking (for example, in the last 10 minutes). To remove a link from your side, call revokeLink(externalId); when a user unlinks from Block-Auth Identity you get the account.unlinked webhook.
Show your UI based on roles (optional)
Not needed for the login to work. If your UI changes with the roles you assign in the Dashboard, read the user with useBlockAuthUser() in React and Vue, or with the BlockAuthService signals in Angular, and check their roles with hasRoleBySlug(slug). You have the example for each framework in Roles, along with how to protect your backend by role.
Read app data from your server (optional, Node.js)
The browser SDK above never needs apiSecret. Administrative operations — reading your app model, users, roles and logs — run on your own server with the BlockAuthProvider of @block-auth.io/core, which does take apiSecret.
This is the only place apiSecret belongs. It is a server-only credential: it lives in a server environment variable, never in frontend code, and never reaches the browser.
// server.js — runs on YOUR Node backend (CommonJS), never bundled into the frontend
const { BlockAuthProvider } = require("@block-auth.io/core");
const provider = new BlockAuthProvider(
process.env.BLOCKAUTH_API_KEY, // publishable key
process.env.BLOCKAUTH_API_SECRET, // server-only secret — keep off the frontend
);
async function main() {
// Connect the backend to the Block-Auth provider (blockchain + API), then authenticate.
await provider.connect();
await provider.signin();
// Privileged, admin-level operations run here on the server:
const app = await provider.app(); // your app model
const users = await provider.users(); // your app's users and their roles
}
On Node, load the SDK with require. Importing it with import from an ES
module currently fails with Named export 'SHA256' not found.
To know which user logged in, don't use these calls with an address sent by your frontend: use the routes from backend routes, where your backend gets the address from Block-Auth.

