> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dloopiq.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Authentification

> Trois types d'utilisateurs, trois méthodes d'authentification.

## Vue d'ensemble

DLoopIQ utilise deux mécanismes d'authentification selon le type d'utilisateur :

| Type            | Mécanisme                           | Header HTTP                      |
| --------------- | ----------------------------------- | -------------------------------- |
| **Acheteur**    | Email + mot de passe → JWT 30 jours | `Authorization: Bearer <jwt>`    |
| **Admin**       | Email + mot de passe → JWT 30 jours | `Authorization: Bearer <jwt>`    |
| **Développeur** | Clé API (cuid)                      | `Authorization: Bearer <apiKey>` |

***

## Authentification Acheteur

### 1. S'inscrire

```bash theme={null}
curl -X POST https://dloopiq.onrender.com/api/v1/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "email": "mon@email.com",
    "password": "MonMotDePasse123!",
    "companyName": "Ma Startup IA"
  }'
```

Réponse :

```json theme={null}
{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "buyer": {
    "id": "cmpkb4ghw00016qduu44p7n2q",
    "email": "mon@email.com",
    "companyName": "Ma Startup IA",
    "creditBalance": 0
  }
}
```

### 2. Se connecter

```bash theme={null}
curl -X POST https://dloopiq.onrender.com/api/v1/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "mon@email.com",
    "password": "MonMotDePasse123!"
  }'
```

### 3. Utiliser le JWT

```bash theme={null}
curl https://dloopiq.onrender.com/api/v1/auth/me \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
```

<Warning>
  Le JWT expire après **30 jours**. Reconnectez-vous via `/auth/login` pour en obtenir un nouveau.
</Warning>

### 4. Changer de mot de passe

```bash theme={null}
curl -X POST https://dloopiq.onrender.com/api/v1/auth/change-password \
  -H "Authorization: Bearer <jwt>" \
  -H "Content-Type: application/json" \
  -d '{
    "currentPassword": "AncienMDP123!",
    "newPassword": "NouveauMDP456!"
  }'
```

***

## Authentification Développeur

Les développeurs s'authentifient avec leur **clé API** (format cuid), générée à la création du compte.

```bash theme={null}
# Récupérer son profil développeur
curl https://dloopiq.onrender.com/api/v1/dev/me \
  -H "Authorization: Bearer cmpkb4ghw00016qduu44p7n2q"
```

### Regénérer sa clé API

Si votre clé est compromise, générez-en une nouvelle :

```bash theme={null}
curl -X POST https://dloopiq.onrender.com/api/v1/auth/regenerate-api-key \
  -H "Authorization: Bearer <jwt>"
```

<Warning>
  L'ancienne clé est **immédiatement invalidée**. Mettez à jour toutes vos intégrations SDK.
</Warning>

***

## Créer un compte développeur (Admin)

Les comptes développeurs ne s'inscrivent pas en self-service. Un admin les crée :

```bash theme={null}
curl -X POST https://dloopiq.onrender.com/api/v1/admin/developers \
  -H "Authorization: Bearer <admin_jwt>" \
  -H "Content-Type: application/json" \
  -d '{
    "email": "dev@monapp.com",
    "appName": "MonApp Mobile",
    "appUrl": "https://monapp.com"
  }'
```

Réponse :

```json theme={null}
{
  "developer": {
    "id": "cmpkb4ghw00016qduu44p7n2r",
    "email": "dev@monapp.com",
    "appName": "MonApp Mobile",
    "apiKey": "cmpkb4ghw00016qduu44p7n2r"
  }
}
```

***

## Sécurité

* Les mots de passe sont hashés avec bcrypt (rounds: 12)
* Les JWTs sont signés HS256 avec `JWT_SECRET` (64 chars hex)
* Les clés API sont des cuid — format opaque, difficiles à deviner
* Toutes les routes sensibles sont rate-limitées (100 req/15min par IP)
* HTTPS obligatoire en production
