# App Users & JWT Authentication

> Decoupled end-user authentication, JWT lifecycles, profile management, and password reset flows.

- **Category:** Authentication
- **Canonical Guide:** https://pottr.tangble.com/docs/app-users

---

>
Decoupled end-user authentication, JWT lifecycles, profile management, and password reset flows.

## How are App Users Different from WordPress Admins?

Pottr provides an independent authentication layer designed specifically for end-users of your custom application:

<ul>
<li>**No WordPress Admin Access:** App users cannot log into `/wp-admin` and have no core WordPress capabilities.</li>
<li>**No `wp_users` Bloat:** App users are stored in dedicated application authentication tables, keeping WordPress administration clean.</li>
<li>**Custom Profile Fields:** Blueprints allow declaring custom fields on user accounts (e.g. `full_name`, `company`, `avatar_url`).</li>
<li>**Stateless JWT Tokens:** Authenticated via 15-minute HS256 JWT access tokens and 30-day hashed refresh tokens.</li>
</ul>

## App Header Requirement (`X-Pottr-App`)

Every authentication endpoint requires an application identifier passed in the `X-Pottr-App: <app_slug>` request header (or an `app` string in the JSON body).

## Authentication Endpoints

### 1. User Signup

POST
/auth/signup
Public Key Required

Registers a new app user. Password must be at least 8 characters. Returns `201 Created` with tokens.

**File: `signup.js`**

```javascript
const res = await fetch('https://example.com/wp-json/pottr/v1/auth/signup', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${PUBLIC_KEY}`,
    'X-Pottr-App': 'task-manager',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    email: 'alex@example.com',
    password: 'securePassword123',
    full_name: 'Alex Smith'
  })
});
```

### 2. User Login

POST
/auth/login
Public Key Required

Authenticates user credentials. Signup and login return the same shape:

**File: `Token Response`**

```json
{
  "user": { "id": 12, "email": "alex@example.com", "full_name": "Alex Smith" },
  "access_token": "eyJ...",
  "refresh_token": "64_character_hex_refresh_token",
  "token_type": "Bearer",
  "expires_in": 900
}
```

Send the access token in the `X-Pottr-User-Token` header (alongside your public key). Passwords are never returned.

### 3. Current User Profile

GET
/auth/me
User JWT Required

### 4. Token Refresh

POST
/auth/refresh
Public Key Required

Refresh tokens are **single-use**. Each refresh returns a new access token *and* a new refresh token; store the new one and discard the old. Presenting an already-used refresh token is treated as theft: Pottr revokes that whole session and returns `401 token_breach_detected`.

**File: `Request Body`**

```text
{
  "refresh_token": "64_character_hex_refresh_token"
}
```

### 5. Logout

POST
/auth/logout
User JWT Required

Revokes the current device session. Pass `?all=true` (or `{ "all": true }`) to sign the user out everywhere. Outstanding access tokens are invalidated immediately.

### 6. Sessions

GET
/auth/sessions
User JWT Required

Lists the user's active device sessions with IP, browser and an `is_current` flag. End one with:

DELETE
/auth/sessions/&#123;family_id&#125;
User JWT Required

## Rate Limits

Signup, login and password-reset routes are rate limited per IP. Exceeding the limit returns `429 rate_limited`.

## Password Reset Flow

<ol>
<li>
**Request Reset Email (`POST /auth/forgot-password`):**
Client submits `{ "email": "user@example.com" }`. Pottr generates a secure 1-hour single-use transient token and sends a reset email via WordPress `wp_mail()`. Always returns `200 { success: true }` to prevent email enumeration.
</li>
<li>
**Complete Password Reset (`POST /auth/reset-password`):**
Client submits `{ "email": "...", "token": "...", "password": "newPassword" }`. Pottr validates the token, updates the password hash, immediately destroys the token, and revokes active sessions.
</li>
</ol>
