Use the session in your routes
Optional. Do it when your backend needs to know who the user is: to protect routes, check roles or join Block-Auth with your own login.
How you add the backend login routes
Applies from here on. You pick it once in the SDK Quickstart.
Your app today
Applies from here on. You pick it once in the SDK Quickstart.
Use the session in your routes
Your usual login stays the same. What changes is how your backend knows who the user is: it can come from your session or from Block-Auth. If the user is linked, Block-Auth gives you their id in your app as externalId. Use a single function for both paths. Use it instead of your current check, also in the route your frontend calls to ask who is signed in (GET /api/auth/me in the example). Change userId to the key where your login keeps the user, and findUser to your own lookup:
Copyimport { expressSessionStore } from '@block-auth.io/node/express'
async function currentAppUser(req) {
if (req.session.userId) return findUser(req.session.userId)
Copy const user = await routes.currentUser({ session: expressSessionStore(req) })
return user?.externalId ? findUser(user.externalId) : null
}
app.get('/api/auth/me', async (req, res) => {
const user = await currentAppUser(req)
return user ? res.json(user) : res.status(401).end()
})
findUser is yours: replace it with your app’s own function (your database, your cache…).
Unlinked users. Whoever signs in with Block-Auth without being linked has a Block-Auth session, but that function returns null for them. Pick what to do: create an account in your app right then, or reject them so only linked users get in. To reject them, add this option to the BlockAuthRoutes from step 2:
export const routes = new BlockAuthRoutes({
apiKey: process.env.BLOCKAUTH_API_KEY,
apiSecret: process.env.BLOCKAUTH_API_SECRET,
appUrl: process.env.APP_URL,
Copy onLogin: (user) => (user.externalId ? undefined : false),
})
The rest of this section (roles and protected routes) is the same for your app.
From now on, the session is the source of truth. Read the user before doing anything that needs a signed-in user or a role. You get null when there is no session, it expired or it was closed by a webhook:
Copyimport { expressSessionStore } from '@block-auth.io/node/express'
const currentUser = (req) => routes.currentUser({ session: expressSessionStore(req) })
const requireRole = (slug) => async (req, res, next) => {
const user = await currentUser(req)
if (!user) return res.status(401).end()
if (!user.roles.some((role) => role.slug === slug)) return res.status(403).end()
next()
}
app.delete('/api/orders/:id', requireRole('acme-x7k2p9'), deleteOrder)
Replace acme-x7k2p9 with the slug of one of your roles: copy it from the Roles tab in the Dashboard, under the role name.
deleteOrder is yours: replace it with your app’s own function (your database, your cache…).
The user has address, did, roles ({ name, slug }), externalId (your user ID, if linked), loggedInAt, expiresAt (Unix seconds) and data. In .NET the roles are also role claims with the role slug. Use the role slug, not the name: see Roles.
To keep your own data in the session, or to refuse a login, add onLogin(user): return an object to save it as data, or false to reject the user (they are also signed out of Block-Auth). In .NET, AuthorizeLogin decides and OnLogin returns the data. onLogout(user) runs when the user logs out.

