From e193588176e330f9cfd79ed4e9191c8546644d49 Mon Sep 17 00:00:00 2001 From: Halis Bal Date: Mon, 23 Oct 2023 16:14:35 +0300 Subject: [PATCH] formatted files --- .../gamereview/controller/AuthController.java | 188 +++++++++--------- .../gamereview/controller/UserController.java | 7 - .../request/VerifyResetCodeRequestDto.java | 4 +- .../com/app/gamereview/model/ResetCode.java | 72 +++---- .../repository/ResetCodeRepository.java | 10 +- .../gamereview/repository/UserRepository.java | 5 +- .../app/gamereview/service/EmailService.java | 25 +-- .../app/gamereview/service/UserService.java | 8 +- 8 files changed, 161 insertions(+), 158 deletions(-) diff --git a/app/backend/src/main/java/com/app/gamereview/controller/AuthController.java b/app/backend/src/main/java/com/app/gamereview/controller/AuthController.java index 35dc9150..7b0c18a1 100644 --- a/app/backend/src/main/java/com/app/gamereview/controller/AuthController.java +++ b/app/backend/src/main/java/com/app/gamereview/controller/AuthController.java @@ -25,95 +25,101 @@ @RequestMapping("/api/auth") public class AuthController { - private final AuthService authService; - private final EmailService emailService; - private final UserService userService; - private final ResetCodeRepository resetCodeRepository; - - @Autowired - public AuthController(AuthService authService, EmailService emailService, UserService userService, ResetCodeRepository resetCodeRepository) { - this.authService = authService; - this.emailService= emailService; - this.userService= userService; - this.resetCodeRepository = resetCodeRepository; - } - - @PostMapping("/register") - public ResponseEntity registerUser(@RequestBody RegisterUserRequestDto registerUserRequestDto){ - User userToCreate = authService.registerUser(registerUserRequestDto); - return ResponseEntity.ok(userToCreate); - } - - @PostMapping("/change-password") - public ResponseEntity changePassword(@RequestBody ChangeUserPasswordRequestDto passwordRequestDto) { - Boolean changePasswordResult = authService.changeUserPassword(passwordRequestDto); - return ResponseEntity.ok(changePasswordResult); - } - - @PostMapping("/login") - public ResponseEntity login(@RequestBody LoginUserRequestDto loginRequest) { - LoginUserResponseDto loginResponse = authService.loginUser(loginRequest); - return ResponseEntity.ok(loginResponse); - } - - @PostMapping("/forgot-password") - public ResponseEntity forgotPassword(@RequestParam String email) { - User user = userService.getUserByEmail(email); - - if (user == null) { - return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found"); - } - - // Generate and save a reset code (you can use UUID or any secure method) - String code = generateResetCode(user.getId()); - - // Send email with reset code - String subject = "Password Reset"; - String message = "Your password reset code is: " + code; - message += "\n The reset code will expire after 24 hours."; - emailService.sendEmail(email, subject, message); - - return ResponseEntity.ok("Reset code sent successfully"); - } - - @PostMapping("/verify-reset-code") - public ResponseEntity verifyResetCode(@RequestBody VerifyResetCodeRequestDto request) { - Optional resetCodeOptional = resetCodeRepository.findByCode(request.getResetCode()); - if (resetCodeOptional.isEmpty() || resetCodeOptional.get().getExpirationDate().before(new Date())) { - // Invalid or expired reset code - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid or expired reset code"); - } - - ResetCode resetCode = resetCodeOptional.get(); - - // Check if the reset code matches the user - String userEmail = userService.getUserById(resetCode.getUserId()).getEmail(); - if (!userEmail.equals(request.getUserEmail())) { - // Reset code does not match the user - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(userEmail+ " " + request.getUserEmail()); - } - - // Reset code is valid, generate a JWT token for the user - String token = JwtUtil.generateToken(userService.getUserById(resetCode.getUserId()).getEmail()); - - // Clear the reset code after generating the token - resetCodeRepository.deleteByUserId(resetCode.getUserId()); - - return ResponseEntity.ok(token); - } - private String generateResetCode(String userId) { - // Check if a reset code exists for the user - ResetCode existingResetCode = resetCodeRepository.findByUserId(userId); - - // If a reset code exists, delete it - if (existingResetCode != null) { - resetCodeRepository.delete(existingResetCode); - } - String code = UUID.randomUUID().toString().replace("-", "").substring(0, 6).toUpperCase(); - - ResetCode resetCode = new ResetCode(code, userId, new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000)); - resetCodeRepository.save(resetCode); - - return code; - } + private final AuthService authService; + + private final EmailService emailService; + + private final UserService userService; + + private final ResetCodeRepository resetCodeRepository; + + @Autowired + public AuthController(AuthService authService, EmailService emailService, UserService userService, + ResetCodeRepository resetCodeRepository) { + this.authService = authService; + this.emailService = emailService; + this.userService = userService; + this.resetCodeRepository = resetCodeRepository; + } + + @PostMapping("/register") + public ResponseEntity registerUser(@RequestBody RegisterUserRequestDto registerUserRequestDto) { + User userToCreate = authService.registerUser(registerUserRequestDto); + return ResponseEntity.ok(userToCreate); + } + + @PostMapping("/change-password") + public ResponseEntity changePassword(@RequestBody ChangeUserPasswordRequestDto passwordRequestDto) { + Boolean changePasswordResult = authService.changeUserPassword(passwordRequestDto); + return ResponseEntity.ok(changePasswordResult); + } + + @PostMapping("/login") + public ResponseEntity login(@RequestBody LoginUserRequestDto loginRequest) { + LoginUserResponseDto loginResponse = authService.loginUser(loginRequest); + return ResponseEntity.ok(loginResponse); + } + + @PostMapping("/forgot-password") + public ResponseEntity forgotPassword(@RequestParam String email) { + User user = userService.getUserByEmail(email); + + if (user == null) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found"); + } + + // Generate and save a reset code (you can use UUID or any secure method) + String code = generateResetCode(user.getId()); + + // Send email with reset code + String subject = "Password Reset"; + String message = "Your password reset code is: " + code; + message += "\n The reset code will expire after 24 hours."; + emailService.sendEmail(email, subject, message); + + return ResponseEntity.ok("Reset code sent successfully"); + } + + @PostMapping("/verify-reset-code") + public ResponseEntity verifyResetCode(@RequestBody VerifyResetCodeRequestDto request) { + Optional resetCodeOptional = resetCodeRepository.findByCode(request.getResetCode()); + if (resetCodeOptional.isEmpty() || resetCodeOptional.get().getExpirationDate().before(new Date())) { + // Invalid or expired reset code + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid or expired reset code"); + } + + ResetCode resetCode = resetCodeOptional.get(); + + // Check if the reset code matches the user + String userEmail = userService.getUserById(resetCode.getUserId()).getEmail(); + if (!userEmail.equals(request.getUserEmail())) { + // Reset code does not match the user + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(userEmail + " " + request.getUserEmail()); + } + + // Reset code is valid, generate a JWT token for the user + String token = JwtUtil.generateToken(userService.getUserById(resetCode.getUserId()).getEmail()); + + // Clear the reset code after generating the token + resetCodeRepository.deleteByUserId(resetCode.getUserId()); + + return ResponseEntity.ok(token); + } + + private String generateResetCode(String userId) { + // Check if a reset code exists for the user + ResetCode existingResetCode = resetCodeRepository.findByUserId(userId); + + // If a reset code exists, delete it + if (existingResetCode != null) { + resetCodeRepository.delete(existingResetCode); + } + String code = UUID.randomUUID().toString().replace("-", "").substring(0, 6).toUpperCase(); + + ResetCode resetCode = new ResetCode(code, userId, new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000)); + resetCodeRepository.save(resetCode); + + return code; + } + } diff --git a/app/backend/src/main/java/com/app/gamereview/controller/UserController.java b/app/backend/src/main/java/com/app/gamereview/controller/UserController.java index 6b8d6b3b..3d1aa0d8 100644 --- a/app/backend/src/main/java/com/app/gamereview/controller/UserController.java +++ b/app/backend/src/main/java/com/app/gamereview/controller/UserController.java @@ -28,13 +28,6 @@ public ResponseEntity> getUsers(GetAllUsersFilterRequestDto filter) { return ResponseEntity.ok(users); } - @GetMapping("/deneme") - public ResponseEntity deneme( - @RequestParam(value = "email", required = true) final String email){ - User users = userService.getUserByEmail(email); - return ResponseEntity.ok((users)); - } - @DeleteMapping("/delete") public ResponseEntity deleteUser(@RequestParam(value = "id", required = true) final String id) { Boolean deleteResult = userService.deleteUserById(id); diff --git a/app/backend/src/main/java/com/app/gamereview/dto/request/VerifyResetCodeRequestDto.java b/app/backend/src/main/java/com/app/gamereview/dto/request/VerifyResetCodeRequestDto.java index f084be5d..12252c08 100644 --- a/app/backend/src/main/java/com/app/gamereview/dto/request/VerifyResetCodeRequestDto.java +++ b/app/backend/src/main/java/com/app/gamereview/dto/request/VerifyResetCodeRequestDto.java @@ -5,8 +5,8 @@ @Getter public class VerifyResetCodeRequestDto { - private String resetCode; + private String resetCode; - private String userEmail; + private String userEmail; } \ No newline at end of file diff --git a/app/backend/src/main/java/com/app/gamereview/model/ResetCode.java b/app/backend/src/main/java/com/app/gamereview/model/ResetCode.java index 2a4caae4..7128d171 100644 --- a/app/backend/src/main/java/com/app/gamereview/model/ResetCode.java +++ b/app/backend/src/main/java/com/app/gamereview/model/ResetCode.java @@ -8,51 +8,53 @@ @Document(collection = "resetCodes") public class ResetCode { - @Id - private String id; + @Id + private String id; - public String getId() { - return id; - } + public String getId() { + return id; + } - public void setId(String id) { - this.id = id; - } + public void setId(String id) { + this.id = id; + } - public String getCode() { - return code; - } + public String getCode() { + return code; + } - public void setCode(String code) { - this.code = code; - } + public void setCode(String code) { + this.code = code; + } - public String getUserId() { - return userId; - } + public String getUserId() { + return userId; + } - public void setUser(String userId) { - this.userId = userId; - } + public void setUser(String userId) { + this.userId = userId; + } - public Date getExpirationDate() { - return expirationDate; - } + public Date getExpirationDate() { + return expirationDate; + } - public void setExpirationDate(Date expirationDate) { - this.expirationDate = expirationDate; - } + public void setExpirationDate(Date expirationDate) { + this.expirationDate = expirationDate; + } - private String code; - @Indexed(unique = true) // Ensures a unique constraint on userId field - private String userId; // ID of the associated user - private Date expirationDate; + private String code; - public ResetCode(String code, String userId, Date expirationDate) { + @Indexed(unique = true) // Ensures a unique constraint on userId field + private String userId; // ID of the associated user - this.code = code; - this.userId = userId; - this.expirationDate = expirationDate; - } + private Date expirationDate; + + public ResetCode(String code, String userId, Date expirationDate) { + + this.code = code; + this.userId = userId; + this.expirationDate = expirationDate; + } } diff --git a/app/backend/src/main/java/com/app/gamereview/repository/ResetCodeRepository.java b/app/backend/src/main/java/com/app/gamereview/repository/ResetCodeRepository.java index efb18f45..37c30813 100644 --- a/app/backend/src/main/java/com/app/gamereview/repository/ResetCodeRepository.java +++ b/app/backend/src/main/java/com/app/gamereview/repository/ResetCodeRepository.java @@ -6,7 +6,11 @@ import java.util.Optional; public interface ResetCodeRepository extends MongoRepository { - ResetCode findByUserId(String userId); - Optional findByCode(String code); - void deleteByUserId(String userId); + + ResetCode findByUserId(String userId); + + Optional findByCode(String code); + + void deleteByUserId(String userId); + } \ No newline at end of file diff --git a/app/backend/src/main/java/com/app/gamereview/repository/UserRepository.java b/app/backend/src/main/java/com/app/gamereview/repository/UserRepository.java index 30cde2e9..a9786afa 100644 --- a/app/backend/src/main/java/com/app/gamereview/repository/UserRepository.java +++ b/app/backend/src/main/java/com/app/gamereview/repository/UserRepository.java @@ -14,6 +14,7 @@ public interface UserRepository extends MongoRepository { Optional findByEmailAndIsDeletedFalse(String email); - @Query("{ 'email' : ?0 }") - Optional findByEmail(String email); + @Query("{ 'email' : ?0 }") + Optional findByEmail(String email); + } diff --git a/app/backend/src/main/java/com/app/gamereview/service/EmailService.java b/app/backend/src/main/java/com/app/gamereview/service/EmailService.java index 2b6e3134..127a1f03 100644 --- a/app/backend/src/main/java/com/app/gamereview/service/EmailService.java +++ b/app/backend/src/main/java/com/app/gamereview/service/EmailService.java @@ -7,22 +7,19 @@ @Service public class EmailService { - @Autowired - private JavaMailSender mailSender; - public void sendEmail(String toEmail, - String subject, - String body - ) { - SimpleMailMessage message = new SimpleMailMessage(); - message.setFrom("bounswe2023.group5@gmail.com"); - message.setTo(toEmail); - message.setText(body); - message.setSubject(subject); - mailSender.send(message); - System.out.println("Mail Send..."); + @Autowired + private JavaMailSender mailSender; + public void sendEmail(String toEmail, String subject, String body) { + SimpleMailMessage message = new SimpleMailMessage(); + message.setFrom("bounswe2023.group5@gmail.com"); + message.setTo(toEmail); + message.setText(body); + message.setSubject(subject); + mailSender.send(message); + System.out.println("Mail Send..."); - } + } } diff --git a/app/backend/src/main/java/com/app/gamereview/service/UserService.java b/app/backend/src/main/java/com/app/gamereview/service/UserService.java index a2438e05..0952546e 100644 --- a/app/backend/src/main/java/com/app/gamereview/service/UserService.java +++ b/app/backend/src/main/java/com/app/gamereview/service/UserService.java @@ -69,10 +69,10 @@ public Boolean deleteUserById(String id) { return false; } - public User getUserByEmail(String email) { - Optional getResult = userRepository.findByEmail(email); + public User getUserByEmail(String email) { + Optional getResult = userRepository.findByEmail(email); - return getResult.orElse(null); - } + return getResult.orElse(null); + } }