# Authentication

User authentication operations

## Register New User

> Creates a new user account. Admin registration requires whitelisted email.

```json
{"openapi":"3.0.3","info":{"title":"Blog API","version":"1.0.0"},"tags":[{"name":"Authentication","description":"User authentication operations"}],"servers":[{"url":"https://blog-api.codewithsadee.com/api/v1","description":"API v1 Base Path"}],"security":[],"paths":{"/auth/register":{"post":{"tags":["Authentication"],"summary":"Register New User","description":"Creates a new user account. Admin registration requires whitelisted email.","operationId":"registerUser","requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/UserInputRequired"}}}},"responses":{"201":{"description":"User registered successfully. Sets refreshToken cookie.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/LoginResponse"}}}},"400":{"$ref":"#/components/responses/BadRequestValidation"},"403":{"description":"Admin registration denied for non-whitelisted email.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"500":{"$ref":"#/components/responses/ServerError"}}}}},"components":{"schemas":{"UserInputRequired":{"type":"object","properties":{"email":{"type":"string","format":"email","description":"User's email address","maxLength":50},"password":{"type":"string","description":"User's password","minLength":8,"writeOnly":true},"role":{"type":"string","enum":["admin","user"],"description":"User role (optional for registration)"}},"required":["email","password"]},"LoginResponse":{"allOf":[{"$ref":"#/components/schemas/AccessTokenResponse"},{"type":"object","properties":{"user":{"$ref":"#/components/schemas/User"}}}]},"AccessTokenResponse":{"type":"object","properties":{"accessToken":{"type":"string","description":"JWT Access Token"}}},"User":{"type":"object","properties":{"_id":{"type":"string","format":"objectid","description":"Unique identifier for the user","readOnly":true},"username":{"type":"string","description":"User's unique username","maxLength":20},"email":{"type":"string","format":"email","description":"User's unique email address","maxLength":50},"role":{"type":"string","enum":["admin","user"],"description":"User role","readOnly":true,"default":"user"},"firstName":{"type":"string","description":"User's first name","maxLength":20},"lastName":{"type":"string","description":"User's last name","maxLength":20},"socialLinks":{"type":"object","properties":{"website":{"type":"string","format":"url","maxLength":100},"facebook":{"type":"string","format":"url","maxLength":100},"instagram":{"type":"string","format":"url","maxLength":100},"linkedin":{"type":"string","format":"url","maxLength":100},"x":{"type":"string","format":"url","maxLength":100},"youtube":{"type":"string","format":"url","maxLength":100}}},"createdAt":{"type":"string","format":"date-time","description":"Timestamp of user creation","readOnly":true},"updatedAt":{"type":"string","format":"date-time","description":"Timestamp of last user update","readOnly":true}},"required":["username","email","role"]},"ValidationErrorResponse":{"type":"object","properties":{"code":{"type":"string","enum":["ValidationError"]},"errors":{"type":"object","additionalProperties":{"$ref":"#/components/schemas/ValidationErrorDetail"}}},"required":["code","errors"]},"ValidationErrorDetail":{"type":"object","properties":{"type":{"type":"string"},"value":{"type":"string"},"msg":{"type":"string"},"path":{"type":"string"},"location":{"type":"string"}}},"ErrorResponse":{"type":"object","properties":{"code":{"type":"string","description":"Application-specific error code"},"message":{"type":"string","description":"Human-readable error message"}},"required":["code","message"]}},"responses":{"BadRequestValidation":{"description":"Invalid input data provided. See errors object for details.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ValidationErrorResponse"}}}},"ServerError":{"description":"An unexpected error occurred on the server.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}}}}}
```

## Login User

> Authenticates a user and returns tokens.

```json
{"openapi":"3.0.3","info":{"title":"Blog API","version":"1.0.0"},"tags":[{"name":"Authentication","description":"User authentication operations"}],"servers":[{"url":"https://blog-api.codewithsadee.com/api/v1","description":"API v1 Base Path"}],"security":[],"paths":{"/auth/login":{"post":{"tags":["Authentication"],"summary":"Login User","description":"Authenticates a user and returns tokens.","operationId":"loginUser","requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/UserInputRequired"}}}},"responses":{"200":{"description":"Login successful. Sets refreshToken cookie.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/LoginResponse"}}}},"400":{"$ref":"#/components/responses/BadRequestValidation"},"404":{"$ref":"#/components/responses/NotFound"},"500":{"$ref":"#/components/responses/ServerError"}}}}},"components":{"schemas":{"UserInputRequired":{"type":"object","properties":{"email":{"type":"string","format":"email","description":"User's email address","maxLength":50},"password":{"type":"string","description":"User's password","minLength":8,"writeOnly":true},"role":{"type":"string","enum":["admin","user"],"description":"User role (optional for registration)"}},"required":["email","password"]},"LoginResponse":{"allOf":[{"$ref":"#/components/schemas/AccessTokenResponse"},{"type":"object","properties":{"user":{"$ref":"#/components/schemas/User"}}}]},"AccessTokenResponse":{"type":"object","properties":{"accessToken":{"type":"string","description":"JWT Access Token"}}},"User":{"type":"object","properties":{"_id":{"type":"string","format":"objectid","description":"Unique identifier for the user","readOnly":true},"username":{"type":"string","description":"User's unique username","maxLength":20},"email":{"type":"string","format":"email","description":"User's unique email address","maxLength":50},"role":{"type":"string","enum":["admin","user"],"description":"User role","readOnly":true,"default":"user"},"firstName":{"type":"string","description":"User's first name","maxLength":20},"lastName":{"type":"string","description":"User's last name","maxLength":20},"socialLinks":{"type":"object","properties":{"website":{"type":"string","format":"url","maxLength":100},"facebook":{"type":"string","format":"url","maxLength":100},"instagram":{"type":"string","format":"url","maxLength":100},"linkedin":{"type":"string","format":"url","maxLength":100},"x":{"type":"string","format":"url","maxLength":100},"youtube":{"type":"string","format":"url","maxLength":100}}},"createdAt":{"type":"string","format":"date-time","description":"Timestamp of user creation","readOnly":true},"updatedAt":{"type":"string","format":"date-time","description":"Timestamp of last user update","readOnly":true}},"required":["username","email","role"]},"ValidationErrorResponse":{"type":"object","properties":{"code":{"type":"string","enum":["ValidationError"]},"errors":{"type":"object","additionalProperties":{"$ref":"#/components/schemas/ValidationErrorDetail"}}},"required":["code","errors"]},"ValidationErrorDetail":{"type":"object","properties":{"type":{"type":"string"},"value":{"type":"string"},"msg":{"type":"string"},"path":{"type":"string"},"location":{"type":"string"}}},"ErrorResponse":{"type":"object","properties":{"code":{"type":"string","description":"Application-specific error code"},"message":{"type":"string","description":"Human-readable error message"}},"required":["code","message"]}},"responses":{"BadRequestValidation":{"description":"Invalid input data provided. See errors object for details.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ValidationErrorResponse"}}}},"NotFound":{"description":"The specified resource was not found.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"ServerError":{"description":"An unexpected error occurred on the server.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}}}}}
```

## Refresh Access Token

> Generates a new access token using the refresh token cookie.

```json
{"openapi":"3.0.3","info":{"title":"Blog API","version":"1.0.0"},"tags":[{"name":"Authentication","description":"User authentication operations"}],"servers":[{"url":"https://blog-api.codewithsadee.com/api/v1","description":"API v1 Base Path"}],"security":[],"paths":{"/auth/refresh-token":{"post":{"tags":["Authentication"],"summary":"Refresh Access Token","description":"Generates a new access token using the refresh token cookie.","operationId":"refreshToken","parameters":[{"$ref":"#/components/parameters/RefreshTokenCookie"}],"responses":{"200":{"description":"Access token refreshed successfully.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/AccessTokenResponse"}}}},"400":{"$ref":"#/components/responses/BadRequestValidation"},"401":{"$ref":"#/components/responses/Unauthorized"},"500":{"$ref":"#/components/responses/ServerError"}}}}},"components":{"parameters":{"RefreshTokenCookie":{"in":"cookie","name":"refreshToken","schema":{"type":"string","format":"jwt"},"required":true,"description":"HTTP-only refresh token cookie."}},"schemas":{"AccessTokenResponse":{"type":"object","properties":{"accessToken":{"type":"string","description":"JWT Access Token"}}},"ValidationErrorResponse":{"type":"object","properties":{"code":{"type":"string","enum":["ValidationError"]},"errors":{"type":"object","additionalProperties":{"$ref":"#/components/schemas/ValidationErrorDetail"}}},"required":["code","errors"]},"ValidationErrorDetail":{"type":"object","properties":{"type":{"type":"string"},"value":{"type":"string"},"msg":{"type":"string"},"path":{"type":"string"},"location":{"type":"string"}}},"ErrorResponse":{"type":"object","properties":{"code":{"type":"string","description":"Application-specific error code"},"message":{"type":"string","description":"Human-readable error message"}},"required":["code","message"]}},"responses":{"BadRequestValidation":{"description":"Invalid input data provided. See errors object for details.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ValidationErrorResponse"}}}},"Unauthorized":{"description":"Authentication information is missing or invalid (e.g., missing/expired token).","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"ServerError":{"description":"An unexpected error occurred on the server.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}}}}}
```

## Logout User

> Invalidates the refresh token and clears the cookie. Requires both access and refresh tokens.

```json
{"openapi":"3.0.3","info":{"title":"Blog API","version":"1.0.0"},"tags":[{"name":"Authentication","description":"User authentication operations"}],"servers":[{"url":"https://blog-api.codewithsadee.com/api/v1","description":"API v1 Base Path"}],"security":[{"bearerAuth":[]}],"components":{"securitySchemes":{"bearerAuth":{"type":"http","scheme":"bearer","bearerFormat":"JWT","description":"JWT Access Token obtained via login/register/refresh"}},"parameters":{"RefreshTokenCookie":{"in":"cookie","name":"refreshToken","schema":{"type":"string","format":"jwt"},"required":true,"description":"HTTP-only refresh token cookie."}},"responses":{"BadRequestValidation":{"description":"Invalid input data provided. See errors object for details.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ValidationErrorResponse"}}}},"Unauthorized":{"description":"Authentication information is missing or invalid (e.g., missing/expired token).","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"ServerError":{"description":"An unexpected error occurred on the server.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}}},"schemas":{"ValidationErrorResponse":{"type":"object","properties":{"code":{"type":"string","enum":["ValidationError"]},"errors":{"type":"object","additionalProperties":{"$ref":"#/components/schemas/ValidationErrorDetail"}}},"required":["code","errors"]},"ValidationErrorDetail":{"type":"object","properties":{"type":{"type":"string"},"value":{"type":"string"},"msg":{"type":"string"},"path":{"type":"string"},"location":{"type":"string"}}},"ErrorResponse":{"type":"object","properties":{"code":{"type":"string","description":"Application-specific error code"},"message":{"type":"string","description":"Human-readable error message"}},"required":["code","message"]}}},"paths":{"/auth/logout":{"post":{"tags":["Authentication"],"summary":"Logout User","description":"Invalidates the refresh token and clears the cookie. Requires both access and refresh tokens.","operationId":"logoutUser","parameters":[{"$ref":"#/components/parameters/RefreshTokenCookie"}],"responses":{"200":{"description":"Logout successful. Clears refreshToken cookie.","content":{"application/json":{"schema":{"type":"object","properties":{"message":{"type":"string"}}}}}},"400":{"$ref":"#/components/responses/BadRequestValidation"},"401":{"$ref":"#/components/responses/Unauthorized"},"500":{"$ref":"#/components/responses/ServerError"}}}}}}
```


---

# Agent Instructions: 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://docs.blog-api.codewithsadee.com/references/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.
