Skip to content

Commit

Permalink
implemented forgot-password endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
UnalDeniz committed Oct 22, 2023
1 parent b413d4c commit 2355e6f
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,35 @@
import com.app.gamereview.dto.request.ChangeUserPasswordRequestDto;
import com.app.gamereview.dto.request.RegisterUserRequestDto;
import com.app.gamereview.dto.response.LoginUserResponseDto;
import com.app.gamereview.model.ResetCode;
import com.app.gamereview.model.User;
import com.app.gamereview.repository.ResetCodeRepository;
import com.app.gamereview.service.AuthService;
import com.app.gamereview.service.EmailService;,
import com.app.gamereview.util.JwtUtil;
import com.app.gamereview.service.EmailService;
import com.app.gamereview.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;
import java.util.Date;
import java.util.UUID;

@RestController
@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) {
public AuthController(AuthService authService, EmailService emailService, UserService userService, ResetCodeRepository resetCodeRepository) {
this.authService = authService;
this.emailService= emailService;
this.userService= userService;
this.resetCodeRepository = resetCodeRepository;
}

@PostMapping("/register")
Expand All @@ -45,9 +53,38 @@ public ResponseEntity<LoginUserResponseDto> login(@RequestBody LoginUserRequestD
return ResponseEntity.ok(loginResponse);
}

@PostMapping("/send-email")
public String sendEmail(@RequestParam String to, @RequestParam String subject, @RequestParam String body) {
emailService.sendEmail(to, subject, body);
return "Email sent successfully!";
@PostMapping("/forgot-password")
public ResponseEntity<String> 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");
}
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;
}
}
58 changes: 58 additions & 0 deletions app/backend/src/main/java/com/app/gamereview/model/ResetCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.app.gamereview.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Date;

@Document(collection = "resetCodes")
public class ResetCode {

@Id
private String id;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getUserId() {
return userId;
}

public void setUser(String userId) {
this.userId = userId;
}

public Date getExpirationDate() {
return 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;

public ResetCode(String code, String userId, Date expirationDate) {

this.code = code;
this.userId = userId;
this.expirationDate = expirationDate;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.app.gamereview.repository;

import com.app.gamereview.model.ResetCode;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface ResetCodeRepository extends MongoRepository<ResetCode, String> {
ResetCode findByUserId(String userId);
ResetCode findByCode(String code);
void deleteByUserId(String userId);
}
6 changes: 6 additions & 0 deletions app/backend/target/classes/application.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
spring.data.mongodb.database=${MONGO_DATABASE}
spring.data.mongodb.uri=mongodb+srv://${MONGO_USER}:${MONGO_PASSWORD}@${MONGO_CLUSTER}
spring.mail.host=${MAIL_HOST}
spring.mail.port=${MAIL_PORT}
spring.mail.username=${MAIL_USERNAME}
spring.mail.password=${MAIL_PASSWORD}
spring.mail.properties.mail.smtp.auth=${MAIL_AUTH}
spring.mail.properties.mail.smtp.starttls.enable=${MAIL_STARTTLS}

0 comments on commit 2355e6f

Please sign in to comment.