EduLascala 1 hónapja
szülő
commit
5f9bf5c855

+ 13 - 0
src/main/java/com/platform2easy/genesis/infra/security/TokenService.java

@@ -35,4 +35,17 @@ public class TokenService {
                 .verify(token);
         return decoded.getSubject();
     }
+    
+    public boolean validateToken(String token) {
+        try {
+            Algorithm algorithm = Algorithm.HMAC256(secret);
+            JWT.require(algorithm)
+                .withIssuer("genesis")
+                .build()
+                .verify(token);
+            return true;
+        } catch (Exception e) {
+            return false;
+        }
+    }
 }

+ 8 - 2
src/main/java/com/platform2easy/genesis/web/controller/AuthenticationController.java

@@ -148,7 +148,13 @@ public class AuthenticationController {
         
         String token = authHeader.substring(7);
         try {
-            String username = tokenService.validateToken(token);
+            // First validate the token
+            if (!tokenService.validateToken(token)) {
+                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
+            }
+            
+            // If token is valid, get the username from the token
+            String username = tokenService.getSubject(token);
             Optional<AppUser> userOpt = appUserRepository.findByUserEmail(username);
             
             if (userOpt.isEmpty()) {
@@ -156,7 +162,7 @@ public class AuthenticationController {
             }
             
             AppUser user = userOpt.get();
-            return ResponseEntity.ok(new TokenDTO(token, user.getCompanyId()));
+            return ResponseEntity.ok(new TokenDTO(token, user.getCompanyId(), user.getUserId()));
             
         } catch (Exception e) {
             return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();

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

@@ -19,8 +19,8 @@ public class CompanyResponseDTO {
         }
         return new CompanyResponseDTO(
             company.getId(),
-            company.getCompanyName(),
-            company.getCompanyFlag()
+            company.getName(),
+            company.getFlag()
         );
     }
 }

+ 13 - 3
src/main/java/com/platform2easy/genesis/web/dto/TokenDTO.java

@@ -1,6 +1,5 @@
 package com.platform2easy.genesis.web.dto;
 
-import lombok.AllArgsConstructor;
 import lombok.Getter;
 import lombok.NoArgsConstructor;
 import lombok.Setter;
@@ -8,12 +7,23 @@ import lombok.Setter;
 @Getter
 @Setter
 @NoArgsConstructor
-@AllArgsConstructor
 public class TokenDTO {
     private String token;
     private Integer companyId;
+    private Long userId;
     
     public TokenDTO(String token) {
         this.token = token;
     }
-}
+    
+    public TokenDTO(String token, Integer companyId) {
+        this.token = token;
+        this.companyId = companyId;
+    }
+    
+    public TokenDTO(String token, Integer companyId, Long userId) {
+        this.token = token;
+        this.companyId = companyId;
+        this.userId = userId;
+    }
+}