فهرست منبع

Add roles to register response

Daniel Bohry 1 سال پیش
والد
کامیت
7d7c683d42

+ 8 - 8
src/main/java/com/danielbohry/authservice/service/auth/AuthService.java

@@ -2,6 +2,7 @@ package com.danielbohry.authservice.service.auth;
 
 import com.danielbohry.authservice.api.dto.AuthenticationRequest;
 import com.danielbohry.authservice.api.dto.AuthenticationResponse;
+import com.danielbohry.authservice.domain.ApplicationUser;
 import com.danielbohry.authservice.service.user.UserService;
 import lombok.AllArgsConstructor;
 import org.springframework.security.authentication.AuthenticationManager;
@@ -35,14 +36,9 @@ public class AuthService implements UserDetailsService {
 
     public AuthenticationResponse signup(AuthenticationRequest request) {
         var user = User.builder().username(request.getUsername()).password(passwordEncoder.encode(request.getPassword())).build();
-        service.save(convert(user));
-        var authentication = jwtService.generateToken(user);
-        return AuthenticationResponse.builder()
-            .token(authentication.token())
-            .expirationDate(authentication.expirationDate())
-            .username(authentication.username())
-            .roles(authentication.authorities())
-            .build();
+        ApplicationUser saved = service.save(convert(user));
+        var authentication = jwtService.generateToken(saved);
+        return buildResponse(authentication);
     }
 
     public AuthenticationResponse signin(AuthenticationRequest request) {
@@ -51,6 +47,10 @@ public class AuthService implements UserDetailsService {
         );
         var user = service.findByUsername(request.getUsername());
         var authentication = jwtService.generateToken(user);
+        return buildResponse(authentication);
+    }
+
+    private static AuthenticationResponse buildResponse(Authentication authentication) {
         return AuthenticationResponse.builder()
             .token(authentication.token())
             .expirationDate(authentication.expirationDate())

+ 2 - 2
src/main/java/com/danielbohry/authservice/service/user/UserService.java

@@ -29,7 +29,7 @@ public class UserService {
                 .orElseThrow(() -> new NotFoundException("User not found"));
     }
 
-    public void save(ApplicationUser applicationUser) {
+    public ApplicationUser save(ApplicationUser applicationUser) {
         validateUsername(applicationUser);
 
         applicationUser.setId(UUID.randomUUID().toString());
@@ -37,7 +37,7 @@ public class UserService {
         applicationUser.setRoles(List.of(USER));
         applicationUser.setActive(true);
 
-        repository.save(applicationUser);
+        return repository.save(applicationUser);
     }
 
     public ApplicationUser update(ApplicationUser applicationUser) {