# Auth event hooks
Source: https://sovaria.in/docs/auth-hooks

Call your own endpoint when users sign up, sign in, fail to sign in, sign out, reset a password, verify their email or are banned: events, payload, verifying the signature, retries and limits.

An auth event hook sends a signed `POST` to your URL when something happens to one of your app users — to sync a CRM, send a welcome email or alert on suspicious sign-ins. Add them under **Users › Auth hooks** or from code.

Hooks receive your users' email addresses, so only the project's **owners and admins** can add, list or remove them. The project is the one you call on (your project URL, or `X-Sovaria-Project-ID`); other team members, app users and API keys get `403`.

| Event | When |
|---|---|
| `auth.user.registered` | A user signed up or was created. |
| `auth.user.login.success` | A sign-in succeeded. |
| `auth.user.login.failed` | A sign-in failed (wrong password, unknown user…). |
| `auth.user.logout` | A user signed out. |
| `auth.user.password_reset` | A password was reset. |
| `auth.user.email_verified` | An email address was confirmed. |
| `auth.user.banned` | A user was banned. |
| `*` | All of the above. |

JavaScript:
```js
await sovaria.auth.eventHooks.create({
  name: 'crm-sync',
  url: 'https://api.example.com/hooks/sovaria',
  events: ['auth.user.registered', 'auth.user.email_verified'],
  secret: process.env.SOVARIA_HOOK_SECRET,   // used to sign each call
})
```

## What your endpoint receives

Every call is a JSON `POST` with the same envelope. `id` is unique per event, so you can ignore a repeat.

auth.user.registered:
```json
{
  "id": "5f3c2a9e-…",
  "type": "auth.user.registered",
  "project_id": "c0ffee00-…",
  "timestamp": "2026-10-20T09:12:44.120Z",
  "data": {
    "user_id": "8d1e…",
    "email": "ada@example.com",
    "org_id": "0f6c…",
    "project_id": "c0ffee00-…",
    "env_id": null
  }
}
```

## Check the signature

Each call carries `X-Sovaria-Signature: sha256=<hex>`, the HMAC-SHA-256 of the raw request body with your secret. Compare it before trusting the body:

Node:
```js
import crypto from 'node:crypto'

export function isFromSovaria(rawBody, header, secret) {
  const expected = 'sha256=' + crypto.createHmac('sha256', secret).update(rawBody).digest('hex')
  return header?.length === expected.length && crypto.timingSafeEqual(Buffer.from(header), Buffer.from(expected))
}
```

Python:
```python
import hashlib, hmac

def is_from_sovaria(raw_body: bytes, header: str, secret: str) -> bool:
    expected = "sha256=" + hmac.new(secret.encode(), raw_body, hashlib.sha256).hexdigest()
    return hmac.compare_digest(header or "", expected)
```

## Delivery

- Each call waits up to 10 seconds for a `2xx`. On a network error or another status it is retried 3 times (after about 0.5, 1 and 2 seconds), then marked failed.
- Return `2xx` quickly and do slow work afterwards; make your handler safe to receive the same event twice.
- URLs must be public HTTPS addresses; private and internal addresses are refused, also if a hostname later resolves to one.
- Secrets are encrypted at rest; a hook's last error is kept as `http_<status>` or `network`, never the response body.
- At most 20 hooks per project.

## Add custom claims to tokens

To refuse a sign-up or sign-in, or add claims at that moment, use [blocking hooks](https://sovaria.in/docs/auth-blocking-hooks) (before create, before sign-in) — they run inside the flow and wait for your answer.

Event hooks tell you what happened; they can't change it. To put your own data into users' access tokens (a team, a plan, a tenant id that your rules read), use the **JWT enrichment hook** under **Users › Security & JWT** (Pro plan and up, plan key `auth.custom_jwt_claims`).

- Sovaria calls your HTTPS endpoint each time it issues a token for the project, with `{"user_id", "project_id", "claims"}` and the same `X-Sovaria-Signature` header.
- Reply with `{"claims": {"team": "growth", "tier": "pro"}}`. The keys are added under `ctx` in the token.
- Keys that would change access are reserved and dropped from your reply: `roles`, `org_id`, `project_id`, `env_id` and `aal`.
- Your endpoint has **50 ms** to answer. If it is slow or fails, the token is issued without your claims and sign-in carries on, so treat the claims as hints and keep a default in your rules.

SQL:
```sql
-- Rows visible only to the user's team, from a claim your hook added
create policy "team rows" on projects for select to authenticated
  using (team = auth.jwt() -> 'ctx' ->> 'team');
```
