From dcbef9a791ef140a0bf6e6fcf29699b71a7e41d0 Mon Sep 17 00:00:00 2001 From: Huynh Nguyen Date: Tue, 5 Nov 2024 10:45:11 +0700 Subject: [PATCH] [NguyenHCP] feat: register account --- .../controller/AuthenticationController.java | 14 ++++++++--- .../Authentication/RegisterRequest.java | 17 +++++++++++++ .../service/AuthenticationService.java | 25 ++++++++++++++++--- 3 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/swp/PodBookingSystem/dto/request/Authentication/RegisterRequest.java diff --git a/src/main/java/com/swp/PodBookingSystem/controller/AuthenticationController.java b/src/main/java/com/swp/PodBookingSystem/controller/AuthenticationController.java index 12a3c397..a1db3721 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/AuthenticationController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/AuthenticationController.java @@ -1,11 +1,8 @@ package com.swp.PodBookingSystem.controller; import com.nimbusds.jose.JOSEException; -import com.swp.PodBookingSystem.dto.request.Authentication.ForgotPasswordRequest; -import com.swp.PodBookingSystem.dto.request.Authentication.LogoutRequest; -import com.swp.PodBookingSystem.dto.request.Authentication.RefreshTokenRequest; +import com.swp.PodBookingSystem.dto.request.Authentication.*; import com.swp.PodBookingSystem.dto.respone.ApiResponse; -import com.swp.PodBookingSystem.dto.request.Authentication.AuthenticationRequest; import com.swp.PodBookingSystem.dto.request.IntrospectRequest; import com.swp.PodBookingSystem.dto.respone.AuthenticationResponse; import com.swp.PodBookingSystem.dto.respone.IntrospectResponse; @@ -59,6 +56,15 @@ ApiResponse login(@RequestBody AuthenticationRequest req .build(); } + @PostMapping("/register") + ApiResponse register(@RequestBody RegisterRequest request) throws ParseException { + var result = authenticationService.register(request); + return ApiResponse.builder() + .message("Đăng kí thành công") + .data(result) + .build(); + } + @PostMapping("/introspect") ApiResponse authenticate(@RequestBody IntrospectRequest request) throws ParseException, JOSEException { diff --git a/src/main/java/com/swp/PodBookingSystem/dto/request/Authentication/RegisterRequest.java b/src/main/java/com/swp/PodBookingSystem/dto/request/Authentication/RegisterRequest.java new file mode 100644 index 00000000..3c73f83d --- /dev/null +++ b/src/main/java/com/swp/PodBookingSystem/dto/request/Authentication/RegisterRequest.java @@ -0,0 +1,17 @@ +package com.swp.PodBookingSystem.dto.request.Authentication; + +import jakarta.validation.constraints.Size; +import lombok.*; +import lombok.experimental.FieldDefaults; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +@FieldDefaults(level = AccessLevel.PRIVATE) +public class RegisterRequest { + @Size(min = 5, message = "NAME_INVALID") + String email; + String password; + String name; +} diff --git a/src/main/java/com/swp/PodBookingSystem/service/AuthenticationService.java b/src/main/java/com/swp/PodBookingSystem/service/AuthenticationService.java index 0b19f8eb..ee53ce37 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/AuthenticationService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/AuthenticationService.java @@ -6,10 +6,7 @@ import com.nimbusds.jwt.JWTClaimsSet; import com.nimbusds.jwt.SignedJWT; import com.swp.PodBookingSystem.dto.request.Account.AccountCreationRequest; -import com.swp.PodBookingSystem.dto.request.Authentication.AuthenticationRequest; -import com.swp.PodBookingSystem.dto.request.Authentication.ForgotPasswordRequest; -import com.swp.PodBookingSystem.dto.request.Authentication.LogoutRequest; -import com.swp.PodBookingSystem.dto.request.Authentication.RefreshTokenRequest; +import com.swp.PodBookingSystem.dto.request.Authentication.*; import com.swp.PodBookingSystem.dto.request.IntrospectRequest; import com.swp.PodBookingSystem.dto.respone.AuthenticationResponse; import com.swp.PodBookingSystem.dto.respone.IntrospectResponse; @@ -97,6 +94,26 @@ public AuthenticationResponse login(AuthenticationRequest request) throws ParseE .build(); } + public AuthenticationResponse register(RegisterRequest payload) throws ParseException { + Optional existingAccount = accountRepository.findByEmail(payload.getEmail()); + if (existingAccount.isPresent()) throw new AppException(ErrorCode.EMAIL_EXISTED); + PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(10); + AccountCreationRequest request = new AccountCreationRequest(payload.getName(), payload.getEmail(), passwordEncoder.encode(payload.getPassword()), 0, "Customer", 1); + Account newAccount = accountMapper.toAccount(request); + accountRepository.save(newAccount); + + var accessToken = generateAccessToken(newAccount); + var refreshToken = generateRefreshToken(newAccount); + SignedJWT decodeRefreshToken = SignedJWT.parse(refreshToken); + refreshTokenRepository.save(new RefreshToken(null, refreshToken, newAccount, decodeRefreshToken.getJWTClaimsSet().getIssueTime(), decodeRefreshToken.getJWTClaimsSet().getExpirationTime())); + var accountResponse = accountMapper.toAccountResponseClient(newAccount); + return AuthenticationResponse.builder() + .accessToken(accessToken) + .refreshToken(refreshToken) + .account(accountResponse) + .build(); + } + @Transactional public void logout(LogoutRequest request) throws ParseException { refreshTokenRepository.deleteByToken(request.getRefreshToken());