Skip to content

Commit

Permalink
Merge pull request #138 from Hoang-Nguyen-Huy/feat/socket
Browse files Browse the repository at this point in the history
[NguyenHCP] feat: register account
  • Loading branch information
nguyenhcp2004 authored Nov 5, 2024
2 parents f3e82cc + dcbef9a commit 643cca9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -59,6 +56,15 @@ ApiResponse<AuthenticationResponse> login(@RequestBody AuthenticationRequest req
.build();
}

@PostMapping("/register")
ApiResponse<AuthenticationResponse> register(@RequestBody RegisterRequest request) throws ParseException {
var result = authenticationService.register(request);
return ApiResponse.<AuthenticationResponse>builder()
.message("Đăng kí thành công")
.data(result)
.build();
}

@PostMapping("/introspect")
ApiResponse<IntrospectResponse> authenticate(@RequestBody IntrospectRequest request)
throws ParseException, JOSEException {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,6 +94,26 @@ public AuthenticationResponse login(AuthenticationRequest request) throws ParseE
.build();
}

public AuthenticationResponse register(RegisterRequest payload) throws ParseException {
Optional<Account> 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());
Expand Down

0 comments on commit 643cca9

Please sign in to comment.