فهرست منبع

fix: Now passing company_id on reponse for Register, Creating a Company, and Login; new: Added a endpoint to validate if the JWT token is still active

EduLascala 1 ماه پیش
والد
کامیت
936bcb1de9

+ 83 - 5
routes.md

@@ -37,8 +37,16 @@ POST {{baseUrl}}/auth/register/default
 ```
 - email = "tester@tooeasy.local" 
 - password = "ChangeMe123!"
+- Response 201 Created:
+```json
+{
+  "token": "<JWT>",
+  "companyId": 1,
+  "userId": 1
+}
+```
 
-## POST {{baseUrl}}/auth/register
+#### POST {{baseUrl}}/auth/register
 - Body (application/json) — fields from `RegisterDTO` (`src/main/java/com/platform2easy/genesis/web/dto/RegisterDTO.java`):
 ```json
 {
@@ -61,9 +69,13 @@ POST {{baseUrl}}/auth/register/default
 ```
 - Response 201 Created:
 ```json
-{ "token": "<JWT>" }
+{
+  "token": "<JWT>",
+  "companyId": 1,
+  "userId": 1
+}
 ```
-## Login
+
 #### POST {{baseUrl}}/auth/login
 - Body — `AuthenticationDTO` (`src/main/java/com/platform2easy/genesis/web/dto/AuthenticationDTO.java`):
 ```json
@@ -71,8 +83,65 @@ POST {{baseUrl}}/auth/register/default
 ```
 - Response 200 OK:
 ```json
-{ "token": "<JWT>" }
+{
+  "token": "<JWT>",
+  "companyId": 1,
+  "userId": 1
+}
+```
+
+#### POST {{baseUrl}}/auth/validate-token
+- Headers:
+  - Authorization: Bearer <JWT>
+- Response 200 OK (if token is valid):
+```json
+{
+  "token": "<JWT>",
+  "companyId": 1,
+  "userId": 1
+}
+```
+- Response 401 Unauthorized (if token is invalid or expired)
+
+## User Management
+
+#### GET {{baseUrl}}/auth/company/{companyId}
+- Headers:
+  - Authorization: Bearer <JWT>
+- Path params: companyId (Integer)
+- Response 200 OK: List of users from the specified company
+
+#### PUT {{baseUrl}}/auth/email/{id}
+- Headers:
+  - Authorization: Bearer <JWT>
+  - Content-Type: application/json
+- Path params: id (Long) - User ID
+- Request body:
+```json
+{
+  "userEmail": "new.email@example.com"
+}
+```
+- Response 200 OK: Updated user object
+
+#### PUT {{baseUrl}}/auth/password/{id}
+- Headers:
+  - Authorization: Bearer <JWT>
+  - Content-Type: application/json
+- Path params: id (Long) - User ID
+- Request body:
+```json
+{
+  "userPassword": "newSecurePassword123"
+}
 ```
+- Response 200 OK: Updated user object with hashed password
+
+#### DELETE {{baseUrl}}/auth/{id}
+- Headers:
+  - Authorization: Bearer <JWT>
+- Path params: id (Long) - User ID to delete
+- Response 204 No Content (on successful deletion)
 
 ## Commodity API (`com.platform2easy.genesis.web.controller.CommodityController`)
 #### Base path: {{baseUrl}}/api/commodity
@@ -107,7 +176,7 @@ Notes:
   - List all orderbook entries.
 
 - GET {{baseUrl}}/api/orderbook?isToken=0|1
-  - Query params: `isToken` (0 or 1). Filters by `orderbook_is_token` (1=true, 0=false).
+  - Query params: `isToken` (0 or 1). Filters by `orderbook_is_token` (1 = true = sell, 0 = false = buy).
 
 - GET {{baseUrl}}/api/orderbook/{id}
   - Path params: id (Long)
@@ -153,6 +222,15 @@ Notes:
   "name": "Minha Empresa",
   "flag": "ACTIVE"
 }
+
+```
+- Response 201 Created:
+```json
+{
+  "id": 1,
+  "name": "Minha Empresa",
+  "flag": "ACTIVE"
+}
 ```
 
 - PUT {{baseUrl}}/api/company/{id}

+ 26 - 3
src/main/java/com/platform2easy/genesis/web/controller/AuthenticationController.java

@@ -56,7 +56,7 @@ public class AuthenticationController {
         user.setUserFlag(dto.getFlag());
         AppUser saved = appUserRepository.save(user);
         String token = tokenService.generateToken(saved);
-        return ResponseEntity.status(HttpStatus.CREATED).body(new TokenDTO(token));
+        return ResponseEntity.status(HttpStatus.CREATED).body(new TokenDTO(token, saved.getCompanyId()));
     }
 
     @PostMapping("/login")
@@ -70,7 +70,7 @@ public class AuthenticationController {
             return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
         }
         String token = tokenService.generateToken(user);
-        return ResponseEntity.ok(new TokenDTO(token));
+        return ResponseEntity.ok(new TokenDTO(token, user.getCompanyId()));
     }
 
     @PostMapping("/register/default")
@@ -105,7 +105,7 @@ public class AuthenticationController {
             status = HttpStatus.CREATED;
         }
         String token = tokenService.generateToken(user);
-        return ResponseEntity.status(status).body(new TokenDTO(token));
+        return ResponseEntity.status(status).body(new TokenDTO(token, user.getCompanyId()));
     }
 
     // GET /auth/company/{companyId}: Lista todos os usuários de uma empresa
@@ -139,4 +139,27 @@ public class AuthenticationController {
     public void delete(@PathVariable Long id) {
         service.deletarPorId(id);
     }
+    
+    @PostMapping("/validate-token")
+    public ResponseEntity<TokenDTO> validateToken(@RequestHeader("Authorization") String authHeader) {
+        if (authHeader == null || !authHeader.startsWith("Bearer ")) {
+            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
+        }
+        
+        String token = authHeader.substring(7);
+        try {
+            String username = tokenService.validateToken(token);
+            Optional<AppUser> userOpt = appUserRepository.findByUserEmail(username);
+            
+            if (userOpt.isEmpty()) {
+                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
+            }
+            
+            AppUser user = userOpt.get();
+            return ResponseEntity.ok(new TokenDTO(token, user.getCompanyId()));
+            
+        } catch (Exception e) {
+            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
+        }
+    }
 }

+ 6 - 3
src/main/java/com/platform2easy/genesis/web/controller/CompanyController.java

@@ -2,11 +2,14 @@ package com.platform2easy.genesis.web.controller;
 
 import com.platform2easy.genesis.domain.model.Company;
 import com.platform2easy.genesis.domain.service.CompanyService;
+import com.platform2easy.genesis.web.dto.CompanyResponseDTO;
 import lombok.AllArgsConstructor;
 import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
+import java.util.stream.Collectors;
 
 @RestController
 @RequestMapping("/api/company")
@@ -28,10 +31,10 @@ public class CompanyController {
     }
 
     @PostMapping
-    @ResponseStatus(HttpStatus.CREATED)
-    public Company create(@RequestBody Company company) {
+    public ResponseEntity<CompanyResponseDTO> create(@RequestBody Company company) {
         company.setId(null);
-        return service.salvar(company);
+        Company savedCompany = service.salvar(company);
+        return ResponseEntity.status(HttpStatus.CREATED).body(CompanyResponseDTO.fromEntity(savedCompany));
     }
 
     @PutMapping("/{id}")

+ 26 - 0
src/main/java/com/platform2easy/genesis/web/dto/CompanyResponseDTO.java

@@ -0,0 +1,26 @@
+package com.platform2easy.genesis.web.dto;
+
+import com.platform2easy.genesis.domain.model.Company;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class CompanyResponseDTO {
+    private Long id;
+    private String companyName;
+    private String companyFlag;
+
+    public static CompanyResponseDTO fromEntity(Company company) {
+        if (company == null) {
+            return null;
+        }
+        return new CompanyResponseDTO(
+            company.getId(),
+            company.getCompanyName(),
+            company.getCompanyFlag()
+        );
+    }
+}

+ 5 - 0
src/main/java/com/platform2easy/genesis/web/dto/TokenDTO.java

@@ -11,4 +11,9 @@ import lombok.Setter;
 @AllArgsConstructor
 public class TokenDTO {
     private String token;
+    private Integer companyId;
+    
+    public TokenDTO(String token) {
+        this.token = token;
+    }
 }