> For the complete documentation index, see [llms.txt](https://cde-inc.gitbook.io/cde-cad-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cde-inc.gitbook.io/cde-cad-documentation/rest-api/authentication.md). Agents can also search and read this documentation through its [MCP server](https://cde-inc.gitbook.io/cde-cad-documentation/~gitbook/mcp).

# Authentication

CDE CAD supports three authentication flows depending on the client type. Choose the method that matches your integration.

***

**1. Discord OAuth Flow**

Used by the web application for user login. The flow proceeds through three stages:

{% hint style="success" %}
**GET →** `/api/auth/discord`
{% endhint %}

Redirects the user to the Discord OAuth consent screen.

{% hint style="success" %}
**GET →** `/api/auth/discord/callback`
{% endhint %}

Handles the redirect from Discord after user authorization. Sets a session cookie.

{% hint style="success" %}
**GET →** `/api/auth/complete-registration`
{% endhint %}

Finalizes user registration after first-time OAuth login.

## **2. JWT Token Authentication**

For programmatic access or tablet clients. Tokens are issued via login endpoints.

{% hint style="info" %}
**POST →** `/api/auth/login`
{% endhint %}

**Request body:**

```
{
  "email": "user@example.com",
  "password": "yourpassword"
}
```

{% hint style="info" %}
**POST →** `/api/auth/tablet-login`
{% endhint %}

**Request body:**

```
{
  "username": "officer_smith",
  "password": "yourpassword",
  "communityId": "community_abc123"
}
```

{% hint style="info" %}
**JWT Token Properties**

* **Algorithm:** HS256
* **Expiry:** 7 days from issuance
* **Payload:** Contains userId, communityId, and role
  {% endhint %}

**Curl example - Login:**

```
curl -X POST https://cdecad.com/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{"email":"user@example.com","password":"yourpassword"}'
```

**Response:**

```
{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "user": {
    "id": "usr_abc123",
    "email": "user@example.com",
    "role": "admin"
  }
}
```

**Using a JWT token in requests:**

```
curl https://cdecad.com/api/civilian \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIs..." \
  -H "x-community-id: community_abc123"
```

## **3. FiveM API Keys**

API keys are designed for FiveM server-to-CAD communication. They are generated from the Admin Panel and carry the `fvm_` prefix.

* Generated from **Admin Panel > FiveM Settings > API Key**
* Prefixed with `fvm_` for identification
* No expiration - valid until revoked or regenerated
* Scoped to the community that generated them

**Using an API key:**

```
curl https://cdecad.com/api/civilian/fivem-civilian/123-45-6789 \
  -H "x-api-key: fvm_abc123def456ghi789"
```

## Comparison: JWT vs API Key

|              |                           |                            |
| ------------ | ------------------------- | -------------------------- |
| **Feature**  | **JWT Token**             | **FiveM API Key**          |
| Use case     | Web / Tablet clients      | FiveM server integration   |
| Header       | Authorization: Bearer ... | x-api-key: fvm\_...        |
| Expiry       | 7 days                    | Never (until revoked)      |
| User context | Yes (userId in payload)   | No (community-scoped only) |
| Generation   | POST /api/auth/login      | Admin Panel UI             |
