> For the complete documentation index, see [llms.txt](https://trident-cas.sabn.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trident-cas.sabn.xyz/api/authentication.md).

# Authentication

**Base URL**: `/auth`

***

**🔐 Authentication Flow**

1. **Initiate Login**: The frontend redirects the user to the respective provider's endpoint (e.g., `/auth/steam`).
2. **Provider Redirect**: The backend redirects the user to the social provider's login page.
3. **Callback Handling**: After a successful login, the provider redirects the user back to the backend's callback URL.
4. **Token Issuance**: The backend authenticates the user, generates a JWT, and redirects the user back to the frontend with the token in the URL (e.g., `?token=...`).
5. **Storage**: The frontend captures the token from the URL, stores it (e.g., `localStorage`), and redirects to a clean URL.
6. **Authorization**: Subsequent requests from the frontend must include the token in the `Authorization` header using the Bearer scheme:

```http
Authorization: Bearer <your_jwt_token>
```

***

**🚀 Endpoints**

**1. GET `/auth/steam`**

Initiates authentication via Steam.

**Response**

* **Redirects**: To the Steam OpenID login page.

**2. GET `/auth/steam/return`**

Callback for Steam authentication.

**Response**

* **Redirects**: To `MAIN_URL?token=<jwt_token>`.

***

**3. GET `/auth/google`**

Initiates authentication via Google.

**Response**

* **Redirects**: To the Google OAuth2 login page.

**4. GET `/auth/google/callback`**

Callback for Google authentication.

**Response**

* **Redirects**: To `MAIN_URL?token=<jwt_token>`.

***

**5. GET `/auth/me`**

Retrieves the currently authenticated user's profile information.

**Request**

* **Headers**: `Authorization: Bearer <token>`

**Response**

* **Status 200 (Success)**: Returns the user object.
* **Status 401 (Unauthorized)**: Missing or invalid token.

***

**6. GET `/auth/logout`**

Stateless logout.

**Response**

* **Status 200 (Success)**: `{ "success": true, "message": "Logged out successfully" }`
* **Note**: Since the system is stateless, the frontend should should delete the stored JWT.

***

**🔌 Socket.io Authentication**

To authenticate a Socket.io connection, pass the token in the `auth` object during connection:

```javascript
const socket = io("http://localhost:4000", {
  auth: {
    token: "your_jwt_token"
  }
});
```

***

**💡 Frontend Integration Example**

```javascript
// Example fetch request with JWT
async function fetchProtectedData() {
    const token = localStorage.getItem("jwt_token");
    
    try {
        const response = await fetch("/auth/me", {
            headers: {
                "Authorization": `Bearer ${token}`
            }
        });
        
        if (response.ok) {
            const data = await response.json();
            console.log("Logged in as:", data.user.username);
        }
    } catch (error) {
        console.error("Auth error:", error);
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://trident-cas.sabn.xyz/api/authentication.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
