--- description: >- Controller for managing user sessions, including retrieving and closing sessions. --- # SessionController ### Controller Description * **Route**: `api/session` * **Authorize**: Requires authenticated user with roles "Admin" or "User" (`[Authorize(Roles = "Admin,User")]`). ### Endpoints #### GetAllSessions * **Description**: Retrieves all active sessions for the authenticated user. * **Route**: `[GET] api/session/all` * **HTTP Method**: `GET` * **Request**: None * **Responses**: * **200 OK**: Returns a list of user sessions. ```json [ { "id": "Guid", "deviceInfo": "string", "createdAt": "DateTime", "expiresAt": "DateTime", "isRevoked": "boolean" } ] ``` * **403 Forbidden**: If user lacks authorization. ```json "string" ``` * **500 Internal Server Error**: Indicates an unexpected error. ```json "Unexpected Error! Please try again later." ``` #### CloseSession * **Description**: Closes a specific session for the authenticated user. * **Route**: `[DELETE] api/session/close/{sessionId}` * **HTTP Method**: `DELETE` * **Request**: * **Path Parameter**: `sessionId` (Guid, required): ID of the session to close. * **Responses**: * **200 OK**: Indicates session was successfully closed. * No body. * **400 Bad Request**: If `sessionId` is invalid or operation fails. ```json "string" ``` * **403 Forbidden**: If user lacks authorization. ```json "string" ``` * **404 Not Found**: If session is not found. ```json "string" ``` * **500 Internal Server Error**: Indicates an unexpected error. ```json "Unexpected Error! Please try again later." ``` #### CloseAllSessions * **Description**: Closes all active sessions for the authenticated user. * **Route**: `[DELETE] api/session/close/all` * **HTTP Method**: `DELETE` * **Request**: None * **Responses**: * **200 OK**: Indicates all sessions were successfully closed. * No body. * **403 Forbidden**: If user lacks authorization. ```json "string" ``` * **500 Internal Server Error**: Indicates an unexpected error. ```json "Unexpected Error! Please try again later." ``` ### Data Models #### SessionDto * **Description**: Data transfer object for session information. * **Structure**: ```json { "id": "Guid", "deviceInfo": "string", "createdAt": "DateTime", "expiresAt": "DateTime", "isRevoked": "boolean" } ``` ### Error Handling * **Invalid User ID**: Handled by `ICurrentUserService`, aborts if invalid. * **Invalid Operations**: Returns `400 Bad Request` for `InvalidOperationException`. * **Unauthorized Access**: Returns `403 Forbidden` for `UnauthorizedAccessException`. * **Resource Not Found**: Returns `404 Not Found` for `NotFoundException`. * **Unexpected Errors**: Caught and returned as `500 Internal Server Error`. ### Logging * Logs warnings for `UnauthorizedAccessException` and `NotFoundException`. * Logs errors for `InvalidOperationException` and unexpected exceptions.