---
title: "User login"
description: "Endpoints your backend uses to log users in with Block-Auth: get a login link, redeem the one-time code, sign the user out and unlink accounts."
url: "/api-user-login/"
language: "en"
---

# User login

Log users in from your backend when there is no SDK for your stack. Block-Auth tells your backend which user logged in, so your app never trusts an address sent by the browser. For the full flow with code, see [PHP, Python, .NET, Node.js](/sdk-integration-server-login.md).

1. Your backend asks for a login link (`/api/auth/authorize`) and redirects the user to it.
2. The user logs in on Block-Auth and comes back to your `redirectUri` with `?code=…&state=…`. If they reject the login, they come back with `?error=access_denied&state=…` and no code: check the `state` and take them back to your login.
3. Your backend redeems the code (`/api/auth/token`) and gets the user's address and roles.

Every endpoint on this page takes your `apiKey` and the [SHA-256 hash of your `apiSecret`](/api-intro.md#hash-secret) in the body. No access token is needed. Responses and errors follow the [common format](/api-intro.md#responses); after 10 requests with wrong credentials in a minute, your IP gets `429 Too many requests`.

---

## Get a login link · `POST /api/auth/authorize`

Returns the URL of the Block-Auth login page for your app. Redirect the user to it: the link must be opened within **5 minutes**, and the user then has **15 minutes** to log in.

### Body

- `apiKey` (string): Your app's API key.
- `apiSecret` (string): SHA-256 hash (hex) of your API secret.
- `redirectUri` (string): Where Block-Auth sends the user back. It must be an `https` URL on your app's validated domain (for example `https://your-app.com/blockauth/callback`). Any path of that host is valid.
- `state` (string): A random value (up to 512 characters) that you keep in the user's session. Block-Auth returns it with the code; check that it matches to protect your callback against CSRF.

**cURL**

```bash
curl -X POST \
  https://wss.block-auth.io/api/auth/authorize \
  -H 'Content-Type: application/json' \
  -d '{
  "apiKey": "your_api_key",
  "apiSecret": "sha256_of_api_secret",
  "redirectUri": "https://example.com/callback",
  "state": "random_value_in_session"
}'
```

> **Placeholders in this code:**
>
> - Replace the `sha256_…` value with the SHA-256 hash (hex) of your API Secret. Compute it on your computer, never on a web page.
> - Replace `your_api_key` with your API Key, or fill in your data in the SDK Quickstart and it is filled in for you.
> - Replace `https://example.com` with your app’s validated domain.

**Response**

```json
{
  "success": true,
  "message": {
    "url": "https://auth.block-auth.io/?t=…",
    "expiresAt": "2026-10-22T12:05:00.000Z"
  }
}
```

---

## Redeem the code · `POST /api/auth/token`

Exchanges the `code` your callback received for the user who logged in. The code is valid for **60 seconds** and can be redeemed **only once**, so redeem it as soon as the callback receives it.

Check the `state` before calling this endpoint. The `address` in the response is the user's identity in your app: use it to create your own session.

### Body

- `apiKey` (string): Your app's API key.
- `apiSecret` (string): SHA-256 hash (hex) of your API secret.
- `code` (string): The `code` query parameter of your callback.
- `redirectUri` (string): The same `redirectUri` you sent to `/api/auth/authorize`.
- `codeVerifier` (string): Only when the code comes from the widget: the `codeVerifier` it sends to your backend with the code.
- `linkExternalId` (string): Optional. Your own ID for the signed-in user, to [link](/sdk-integration-server-extras.md#link-users) their account to this Block-Auth identity. Up to 255 characters: letters, digits and `. _ : | -`.

### Response

- `address` (string): The user's address. Same value as `did`.
- `signedAt` (string): When the user signed into your app, in UTC with the format `YYYY-MM-DD HH:mm:ss`.
- `signedExpiredAt` (string): When that signature expires, in the same format. Your session should not outlive it.
- `roles` (array): The user's roles in your app, as `{ slug, name }`.
- `externalId` (string): Your own ID for this user if their account is linked, or `null`.

**cURL**

```bash
curl -X POST \
  https://wss.block-auth.io/api/auth/token \
  -H 'Content-Type: application/json' \
  -d '{
  "apiKey": "your_api_key",
  "apiSecret": "sha256_of_api_secret",
  "code": "code_from_your_callback",
  "redirectUri": "https://example.com/callback"
}'
```

> **Placeholders in this code:**
>
> - Replace the `sha256_…` value with the SHA-256 hash (hex) of your API Secret. Compute it on your computer, never on a web page.
> - Replace `your_api_key` with your API Key, or fill in your data in the SDK Quickstart and it is filled in for you.
> - Replace `https://example.com` with your app’s validated domain.
> - Replace `code_from_your_callback` with the `code` Block-Auth sends to your callback URL.

**Response**

```json
{
  "success": true,
  "message": {
    "address": "0x7F85…46D8",
    "did": "0x7F85…46D8",
    "signedNonce": 3,
    "signedAt": "2026-10-22 11:58:41",
    "signedExpiredAt": "2026-10-22 12:58:41",
    "roles": [
      { "slug": "acme-x7k2p9", "name": "Admin" }
    ],
    "externalId": null
  }
}
```

> **Placeholders in this code:**
>
> - Replace `acme-x7k2p9` with the slug of one of your roles: copy it from the Roles tab in the Dashboard, under the role name.

---

## Sign the user out · `POST /api/auth/signout`

Call it when the user logs out of your app. It closes the user's Block-Auth sessions for your app, on every device, so the next login asks for authorization again.

An unknown address answers the same as a known one with no open session: `closed: 0`.

### Body

- `apiKey` (string): Your app's API key.
- `apiSecret` (string): SHA-256 hash (hex) of your API secret.
- `address` (string): The `address` you got from `/api/auth/token`.

**cURL**

```bash
curl -X POST \
  https://wss.block-auth.io/api/auth/signout \
  -H 'Content-Type: application/json' \
  -d '{
  "apiKey": "your_api_key",
  "apiSecret": "sha256_of_api_secret",
  "address": "0x7F85…46D8"
}'
```

> **Placeholders in this code:**
>
> - Replace the `sha256_…` value with the SHA-256 hash (hex) of your API Secret. Compute it on your computer, never on a web page.
> - Replace `your_api_key` with your API Key, or fill in your data in the SDK Quickstart and it is filled in for you.

**Response**

```json
{
  "success": true,
  "message": {
    "closed": 1
  }
}
```

---

## Unlink an account · `POST /api/auth/link/revoke`

Only if you use [account linking](/sdk-integration-server-extras.md#link-users): removes the link between one of your users and their Block-Auth identity, for example when the user deletes their account in your app. The next time they log in with Block-Auth, they have to link again.

`revoked` is `false` when that user had no active link.

### Body

- `apiKey` (string): Your app's API key.
- `apiSecret` (string): SHA-256 hash (hex) of your API secret.
- `externalId` (string): Your own ID for the user, the one you used when linking. Up to 255 characters: letters, digits and `. _ : | -`.

**cURL**

```bash
curl -X POST \
  https://wss.block-auth.io/api/auth/link/revoke \
  -H 'Content-Type: application/json' \
  -d '{
  "apiKey": "your_api_key",
  "apiSecret": "sha256_of_api_secret",
  "externalId": "user-1234"
}'
```

> **Placeholders in this code:**
>
> - Replace the `sha256_…` value with the SHA-256 hash (hex) of your API Secret. Compute it on your computer, never on a web page.
> - Replace `your_api_key` with your API Key, or fill in your data in the SDK Quickstart and it is filled in for you.

**Response**

```json
{
  "success": true,
  "message": {
    "revoked": true
  }
}
```
