|
| 1 | +--- |
| 2 | +title: 'API Reference: Authentication' |
| 3 | +description: Detailed documentation for the authentication endpoints. |
| 4 | +--- |
| 5 | + |
| 6 | +import { Steps, Tabs, TabItem, Aside } from '@astrojs/starlight/components'; |
| 7 | + |
| 8 | +This section provides a detailed reference for all authentication-related endpoints. |
| 9 | + |
| 10 | +## Endpoints |
| 11 | + |
| 12 | +### Anonymous Sign-In |
| 13 | + |
| 14 | +Creates a new anonymous guest user account. This is typically the first call an app makes on a fresh install to allow users to interact with the app before creating a permanent account. |
| 15 | + |
| 16 | +**Endpoint:** `POST /api/v1/auth/anonymous` |
| 17 | + |
| 18 | +**Authentication:** None required. |
| 19 | + |
| 20 | +#### Success Response (200 OK) |
| 21 | + |
| 22 | +Returns a `SuccessApiResponse` containing an `AuthSuccessResponse` payload with the new anonymous `User` object and a JWT `token`. |
| 23 | + |
| 24 | +<Tabs> |
| 25 | +<TabItem label="Payload"> |
| 26 | +```json |
| 27 | +{ |
| 28 | + "data": { |
| 29 | + "user": { |
| 30 | + "id": "66a21e2a77f294e7a1f7d41a", |
| 31 | + |
| 32 | + "appRole": "guestUser", |
| 33 | + "dashboardRole": "none", |
| 34 | + "createdAt": "2024-07-25T09:30:18.000Z", |
| 35 | + "feedActionStatus": { |
| 36 | + // ... default statuses |
| 37 | + } |
| 38 | + }, |
| 39 | + "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." |
| 40 | + }, |
| 41 | + "metadata": { |
| 42 | + // ... |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | +</TabItem> |
| 47 | +</Tabs> |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +### Request Sign-In Code |
| 52 | + |
| 53 | +Initiates the passwordless sign-in/sign-up process by sending a one-time verification code to the user's email. |
| 54 | + |
| 55 | +**Endpoint:** `POST /api/v1/auth/request-code` |
| 56 | + |
| 57 | +**Authentication:** None required. |
| 58 | + |
| 59 | +#### Request Body |
| 60 | + |
| 61 | +<Tabs> |
| 62 | +<TabItem label="Standard Sign-In"> |
| 63 | +```json |
| 64 | +{ |
| 65 | + |
| 66 | +} |
| 67 | +``` |
| 68 | +</TabItem> |
| 69 | +<TabItem label="Dashboard Login"> |
| 70 | +```json |
| 71 | +{ |
| 72 | + |
| 73 | + "isDashboardLogin": true |
| 74 | +} |
| 75 | +``` |
| 76 | +</TabItem> |
| 77 | +</Tabs> |
| 78 | + |
| 79 | +- `email` (string, required): The user's email address. |
| 80 | +- `isDashboardLogin` (boolean, optional): When `true`, the server first verifies the user exists and has the necessary permissions (`admin` or `publisher`) before sending a code. Defaults to `false`. |
| 81 | + |
| 82 | +#### Success Response (202 Accepted) |
| 83 | + |
| 84 | +An empty body with a `202 Accepted` status code indicates the request was successful and the email is being sent. |
| 85 | + |
| 86 | +#### Error Responses |
| 87 | + |
| 88 | +- `400 Bad Request`: If the email format is invalid or the request body is malformed. |
| 89 | +- `401 Unauthorized`: If `isDashboardLogin` is true and the user does not exist. |
| 90 | +- `403 Forbidden`: If `isDashboardLogin` is true and the user exists but lacks dashboard permissions. |
| 91 | + |
| 92 | +--- |
| 93 | + |
| 94 | +### Verify Sign-In Code |
| 95 | + |
| 96 | +Completes the sign-in process by verifying the email and code. This single endpoint handles multiple scenarios: |
| 97 | + |
| 98 | +- Standard user sign-in. |
| 99 | +- New user sign-up. |
| 100 | +- Converting an anonymous guest user to a permanent account. |
| 101 | +- Dashboard user login. |
| 102 | + |
| 103 | +**Endpoint:** `POST /api/v1/auth/verify-code` |
| 104 | + |
| 105 | +**Authentication:** |
| 106 | +- **Optional:** If a guest user is converting their account, include their anonymous JWT `Bearer` token in the `Authorization` header. |
| 107 | +- **None:** For standard sign-in/sign-up flows. |
| 108 | + |
| 109 | +#### Request Body |
| 110 | + |
| 111 | +```json |
| 112 | +{ |
| 113 | + |
| 114 | + "code": "123456", |
| 115 | + "isDashboardLogin": false |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +- `email` (string, required): The user's email address. |
| 120 | +- `code` (string, required): The 6-digit verification code sent to the user's email. |
| 121 | +- `isDashboardLogin` (boolean, optional): Must be `true` if verifying a code for dashboard access. Defaults to `false`. |
| 122 | + |
| 123 | +#### Success Response (200 OK) |
| 124 | + |
| 125 | +Returns a `SuccessApiResponse` containing an `AuthSuccessResponse` payload with the authenticated `User` object and a new JWT `token`. |
| 126 | + |
| 127 | +#### Error Responses |
| 128 | + |
| 129 | +- `400 Bad Request`: If the code is invalid, expired, or the request body is malformed. |
| 130 | +- `403 Forbidden`: If `isDashboardLogin` is true and the user lacks dashboard permissions. |
| 131 | + |
| 132 | +--- |
| 133 | + |
| 134 | +### Get Current User |
| 135 | + |
| 136 | +Retrieves the full profile of the currently authenticated user. |
| 137 | + |
| 138 | +**Endpoint:** `GET /api/v1/auth/me` |
| 139 | + |
| 140 | +**Authentication:** Required. A valid JWT `Bearer` token must be provided. |
| 141 | + |
| 142 | +#### Success Response (200 OK) |
| 143 | + |
| 144 | +Returns a `SuccessApiResponse` containing the `User` object. |
| 145 | + |
| 146 | +<Tabs> |
| 147 | +<TabItem label="Payload"> |
| 148 | +```json |
| 149 | +{ |
| 150 | + "data": { |
| 151 | + "id": "66a21e2a77f294e7a1f7d41a", |
| 152 | + |
| 153 | + "appRole": "standardUser", |
| 154 | + "dashboardRole": "none", |
| 155 | + // ... other user fields |
| 156 | + }, |
| 157 | + "metadata": { |
| 158 | + // ... |
| 159 | + } |
| 160 | +} |
| 161 | +``` |
| 162 | +</TabItem> |
| 163 | +</Tabs> |
| 164 | + |
| 165 | +#### Error Responses |
| 166 | + |
| 167 | +- `401 Unauthorized`: If the token is missing, invalid, or expired. |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +### Sign Out |
| 172 | + |
| 173 | +Performs server-side sign-out actions, primarily invalidating the current JWT to prevent its reuse. |
| 174 | + |
| 175 | +**Endpoint:** `POST /api/v1/auth/sign-out` |
| 176 | + |
| 177 | +**Authentication:** Required. A valid JWT `Bearer` token must be provided. |
| 178 | + |
| 179 | +#### Success Response (204 No Content) |
| 180 | + |
| 181 | +An empty body with a `204 No Content` status code indicates successful sign-out. |
| 182 | + |
| 183 | +#### Error Responses |
| 184 | + |
| 185 | +- `401 Unauthorized`: If the token is missing, invalid, or expired. |
| 186 | + |
| 187 | +--- |
| 188 | + |
| 189 | +### Delete Account |
| 190 | + |
| 191 | +Permanently deletes the authenticated user's account and all associated data (settings, preferences, etc.). |
| 192 | + |
| 193 | +**Endpoint:** `DELETE /api/v1/auth/delete-account` |
| 194 | + |
| 195 | +**Authentication:** Required. A valid JWT `Bearer` token must be provided. |
| 196 | + |
| 197 | +#### Success Response (204 No Content) |
| 198 | + |
| 199 | +An empty body with a `204 No Content` status code indicates successful account deletion. |
| 200 | + |
| 201 | +#### Error Responses |
| 202 | + |
| 203 | +- `401 Unauthorized`: If the token is missing, invalid, or expired. |
| 204 | +- `404 Not Found`: If the user to be deleted does not exist. |
0 commit comments