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

# Authentication

> JWT-based authentication API

## Overview

Wryft Chat uses JWT (JSON Web Tokens) for authentication. All authenticated endpoints require a valid JWT token in the Authorization header.

## Register

Create a new user account.

```http theme={null}
POST /api/auth/register
```

### Request Body

```json theme={null}
{
  "username": "user123",
  "email": "user@example.com",
  "password": "SecurePassword123!"
}
```

### Response

```json theme={null}
{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "user": {
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "username": "user123",
    "email": "user@example.com",
    "created_at": "2024-01-01T00:00:00Z"
  }
}
```

### Validation Rules

* Username: 3-32 characters, alphanumeric + underscores
* Email: Valid email format
* Password: Minimum 8 characters

## Login

Authenticate with existing credentials.

```http theme={null}
POST /api/auth/login
```

### Request Body

```json theme={null}
{
  "email": "user@example.com",
  "password": "SecurePassword123!"
}
```

### Response

```json theme={null}
{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "user": {
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "username": "user123",
    "email": "user@example.com",
    "avatar": "https://...",
    "is_premium": false
  }
}
```

## Using JWT Tokens

Include the token in the Authorization header for all authenticated requests:

```http theme={null}
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
```

### Example with cURL

```bash theme={null}
curl -H "Authorization: Bearer YOUR_TOKEN" \
  https://api.wryft.chat/api/users/me
```

### Example with JavaScript

```javascript theme={null}
const response = await fetch('https://api.wryft.chat/api/users/me', {
  headers: {
    'Authorization': `Bearer ${token}`
  }
});
```

## Token Expiration

JWT tokens expire after 30 days. When a token expires, the user must log in again.

### Checking Token Validity

```http theme={null}
GET /api/users/me
```

Returns 401 Unauthorized if token is invalid or expired.

## Error Responses

### 400 Bad Request

```json theme={null}
{
  "error": "Invalid email format"
}
```

### 401 Unauthorized

```json theme={null}
{
  "error": "Invalid credentials"
}
```

### 409 Conflict

```json theme={null}
{
  "error": "Email already registered"
}
```

## Security Best Practices

<CardGroup cols={2}>
  <Card title="Store Securely" icon="lock">
    Store tokens in httpOnly cookies or secure storage
  </Card>

  <Card title="HTTPS Only" icon="shield">
    Always use HTTPS in production
  </Card>

  <Card title="Token Rotation" icon="rotate">
    Implement token refresh for long sessions
  </Card>

  <Card title="Logout" icon="right-from-bracket">
    Clear tokens on logout
  </Card>
</CardGroup>

## Rate Limiting

Authentication endpoints are rate limited:

* 10 requests per minute per IP
* Returns 429 Too Many Requests when exceeded

## Next Steps

<CardGroup cols={2}>
  <Card title="Users API" icon="user" href="/api/users">
    Manage user profiles
  </Card>

  <Card title="Messages API" icon="message" href="/api/messages">
    Send and receive messages
  </Card>
</CardGroup>
