Receive webhooks

Optional, recommended for production. Login works without it, but if the user signs out from Block-Auth (Identity, the extension or another device), your app won’t know.

How you add the backend login routes

Applies from here on. You pick it once in the SDK Quickstart.

Receive webhooks

Block-Auth calls your backend when a user signs out of your app from Block-Auth (signout) or unlinks your app (account.unlinked), so you can close their session too.

This is how a sign-out made in Block-Auth (Identity, the extension or another device) reaches your page:

  1. 1Block-AuthThe user signs out
  2. 2Block-Auth → Your backendPOST /webhook · signout
  3. 3Your backendSaves the sign-out
  4. 4Your frontend → Your backendGET /me on focus or every 60 s
  5. 5Your backend → Your frontend401
  6. 6Your frontendShows the user signed out
  1. In the Dashboard, open your application's Authentication settings. In the third URL field, shown as "When user fails to log in", type the path /auth/blockauth/webhook (your domain is already filled in).
  1. Add these options to the BlockAuthRoutes from step 2, so the package knows how to remember a sign-out and check it. POST /webhook is already there: it verifies the request and calls onSignout or onUnlinked. Then isRevoked closes every session that started before that sign-out:
Your server · 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,
  onSignout: (event) => saveSignout(event.address, event.iat),
  onUnlinked: async (event) => {
    await saveSignout(event.address, event.iat)
    await removeLink(event.externalId)
  },
  isRevoked: async (user) => ((await lastSignout(user.address.toLowerCase())) ?? -1) >= user.loggedInAt,
})

saveSignout, lastSignout, removeLink are yours: replace them with your app’s own functions (your database, your cache…).

saveSignout, lastSignout and removeLink are yours: keep them in your database or a shared cache such as Redis, so every server instance sees them. address arrives in lowercase and iat and loggedInAt are Unix seconds.

  • isRevoked runs on every GET /me and every time you read the user. The widget asks /me when the tab gets focus and every minute, so the page signs out by itself.
  • To reject a repeated delivery, add isReplay(jti) and return true if you already saw that jti.