Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 143 additions & 25 deletions docs/api/authentication.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,99 @@
# Authentication API

The Authentication API handles user registration, login, profile retrieval, and role-based access. All protected endpoints require a **JWT Bearer Token**, which must be included in the request headers as:
The Authentication API handles user registration, login, profile retrieval, and role-based access. All protected endpoints require a JWT Bearer Token, which must be included in the request headers as:

```
```javascript
Authorization: Bearer <your_token_here>
```

If the token is missing or invalid, the API will return a `401 Unauthorized` response.

---
***

## Base URL

```
```javascript
/api/auth
```

---
***

## Endpoints Overview

| Method | Endpoint | Description | Auth Required |
|--------|----------|-------------|---------------|
| POST | `/api/auth/register` | Register a new user | No |
| POST | `/api/auth/login` | Authenticate an existing user and get a token | No |
| GET | `/profile` | Get details of the logged-in user | Yes |
| POST | `/invite-admin` | Create a new admin user (admin-only route) | Yes (Admin) |

---
<table isTableHeaderOn="true" columnWidths="[object Object]">
<tr>
<td>
<p>Method</p>
</td>
<td>
<p>Endpoint</p>
</td>
<td>
<p>Description</p>
</td>
<td>
<p>Auth Required</p>
</td>
</tr>
<tr>
<td>
<p>POST</p>
</td>
<td>
<p><code>/api/auth/register</code></p>
</td>
<td>
<p>Register a new user</p>
</td>
<td>
<p>No</p>
</td>
</tr>
<tr>
<td>
<p>POST</p>
</td>
<td>
<p><code>/api/auth/login</code></p>
</td>
<td>
<p>Authenticate an existing user and get a token</p>
</td>
<td>
<p>No</p>
</td>
</tr>
<tr>
<td>
<p>GET</p>
</td>
<td>
<p><code>/profile</code></p>
</td>
<td>
<p>Get details of the logged-in user</p>
</td>
<td>
<p>Yes</p>
</td>
</tr>
<tr>
<td>
<p>POST</p>
</td>
<td>
<p><code>/invite-admin</code></p>
</td>
<td>
<p>Create a new admin user (admin-only route)</p>
</td>
<td>
<p>Yes (Admin)</p>
</td>
</tr>
</table>

***

## 1. Register User

Expand Down Expand Up @@ -59,7 +125,7 @@ Registers a new user into the system.
}
```

---
***

## 2. Login User

Expand Down Expand Up @@ -90,7 +156,7 @@ Authenticates a user and returns a JWT token required for accessing protected en
}
```

---
***

## 3. Get Current Authenticated User

Expand All @@ -100,7 +166,7 @@ Returns profile information of the currently logged-in user. This is useful for

### Headers

```
```javascript
Authorization: Bearer <token>
```

Expand All @@ -116,7 +182,7 @@ Authorization: Bearer <token>
}
```

---
***

## 4. Invite Admin (Admin Only)

Expand All @@ -126,7 +192,7 @@ Allows an existing admin to create another admin user.

### Headers

```
```javascript
Authorization: Bearer <admin_token>
```

Expand Down Expand Up @@ -154,13 +220,65 @@ Authorization: Bearer <admin_token>
}
```

---
***

## Common Error Responses

| Status | Reason | Example |
|--------|--------|---------|
| 400 | Missing fields | `{"message": "Email and password required"}` |
| 401 | Invalid or missing token | `{"message": "Not authorized"}` |
| 403 | User does not have permission | `{"message": "Access denied, admin only"}` |
| 409 | Email already registered | `{"message": "User already exists"}` |
<table isTableHeaderOn="true" columnWidths="[object Object]">
<tr>
<td>
<p>Status</p>
</td>
<td>
<p>Reason</p>
</td>
<td>
<p>Example</p>
</td>
</tr>
<tr>
<td>
<p>400</p>
</td>
<td>
<p>Missing fields</p>
</td>
<td>
<p><code>{"message": "Email and password required"}</code></p>
</td>
</tr>
<tr>
<td>
<p>401</p>
</td>
<td>
<p>Invalid or missing token</p>
</td>
<td>
<p><code>{"message": "Not authorized"}</code></p>
</td>
</tr>
<tr>
<td>
<p>403</p>
</td>
<td>
<p>User does not have permission</p>
</td>
<td>
<p><code>{"message": "Access denied, admin only"}</code></p>
</td>
</tr>
<tr>
<td>
<p>409</p>
</td>
<td>
<p>Email already registered</p>
</td>
<td>
<p><code>{"message": "User already exists"}</code></p>
</td>
</tr>
</table>