Documentation

Sign-in hooks

Run your own code before registration, after sign-in and before tokens: refuse, or add claims.

All documentation

Register an HTTPS endpoint under Sign-in hooks in the Auth admin. Auth calls it at one point of every sign-in and waits at most its timeout (500 to 10,000 ms).

TriggerWhenMay
pre_registrationSomeone signs up on a hosted page, before the verification emailrefuse
post_loginAfter the person proved who they are, before the application gets a coderefuse
pre_token_issueBefore every token: code, refresh, device and machine tokensadd claims, or refuse

The call and the answer

Auth posts a JSON event signed like a webhook: check it with verifyWebhook and the hook's own secret, shown once when you add the hook. Answer with a decision.

A hook (Node)
import { verifyWebhook } from '@smartdesk/auth-sdk'

app.post('/hooks/sign-in', express.text({ type: 'application/json' }), async (req, res) => {
  const ok = await verifyWebhook({ secret: process.env.HOOK_SECRET, body: req.body,
    timestamp: req.get('Ziqme-Webhook-Timestamp'), signature: req.get('Ziqme-Webhook-Signature') })
  if (!ok) return res.sendStatus(401)
  const event = JSON.parse(req.body)
  if (event.type === 'post_login' && await trialEnded(event.user.id)) {
    return res.json({ decision: 'deny', reason: 'Your trial has ended.' })
  }
  if (event.type === 'pre_token_issue') return res.json({ decision: 'allow', claims: { plan: await planOf(event.user.id) } })
  res.json({ decision: 'allow' })
})
  • A refusal stops the sign-in; its reason is shown to the person or sent to the application as access_denied.
  • Claims are added to the access and ID tokens, at most 20, never over a claim Auth sets itself.
  • A hook that times out or errors is skipped, or, when it fails closed, the sign-in is refused with a generic message.
  • Every refusal and failure is in the audit log.