Skip to content

Commit

Permalink
feat: verify otp
Browse files Browse the repository at this point in the history
  • Loading branch information
thutasann committed Jan 18, 2025
1 parent 9acd86c commit c1fd7a7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -16,6 +15,7 @@

import com.thuta.trading_backend.entity.User;
import com.thuta.trading_backend.request.LoginRequest;
import com.thuta.trading_backend.request.OTPVerifyRequest;
import com.thuta.trading_backend.response.AuthResponse;
import com.thuta.trading_backend.response.DataResponse;
import com.thuta.trading_backend.service.auth.IAuthService;
Expand Down Expand Up @@ -58,9 +58,13 @@ public ResponseEntity<DataResponse> signIn(@RequestBody LoginRequest request) {
}

@PostMapping("/verify")
public ResponseEntity<AuthResponse> verifySignInOtp(
@PathVariable String otp,
@PathVariable String id) {
return null;
public ResponseEntity<DataResponse> verifySignInOtp(@RequestBody OTPVerifyRequest request) {
try {
AuthResponse authResponse = authService.verifySignInOtp(request.getOtp(), request.getId());
return ResponseEntity.ok(new DataResponse("Verify OTP Success", authResponse));
} catch (Exception e) {
return ResponseEntity.status(INTERNAL_SERVER_ERROR)
.body(new DataResponse("An unexpected error occurred", e.getMessage()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.thuta.trading_backend.request;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class OTPVerifyRequest {
private String otp;

private String id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;

@Service
public class AuthService implements IAuthService {
Expand Down Expand Up @@ -82,7 +81,6 @@ public AuthResponse register(User user) throws Exception {
}

@Override
@Transactional
public AuthResponse signIn(LoginRequest request) throws Exception {
if (request == null) {
throw new IllegalArgumentException("Login Request cannot be null");
Expand Down Expand Up @@ -121,10 +119,34 @@ public AuthResponse signIn(LoginRequest request) throws Exception {
AuthResponse authResponse = new AuthResponse();
authResponse.setJwt(jwt);
authResponse.setStatus(true);
authResponse.setMessage("SignIn sucess");

return authResponse;
}

@Override
public AuthResponse verifySignInOtp(String otp, String id) throws Exception {
if (otp == null || id == null) {
throw new IllegalArgumentException("Verify Request is invalid");
}

TwoFactorOTP twoFactorOTP = twoFactorOtpService.findById(id);

if (twoFactorOTP == null) {
throw new Exception("OTP not found");
}

if (twoFactorOtpService.verifyTwoFactorOtp(twoFactorOTP, otp)) {
AuthResponse authResponse = new AuthResponse();
authResponse.setMessage("Two factor authentication verified");
authResponse.setTwoFactorAuthEnabled(true);
authResponse.setJwt(twoFactorOTP.getJwt());
return authResponse;
}

throw new Exception("Invalid OTP");
}

/**
* Authenticate user
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public interface IAuthService {
AuthResponse register(User user) throws Exception;

AuthResponse signIn(LoginRequest request) throws Exception;

AuthResponse verifySignInOtp(String otp, String id) throws Exception;
}

0 comments on commit c1fd7a7

Please sign in to comment.