mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-22 22:21:09 +08:00
feat: add email verification
This commit is contained in:
@@ -8,6 +8,7 @@ import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/auth")
|
||||
@@ -25,9 +26,17 @@ public class AuthController {
|
||||
@PostMapping("/register")
|
||||
public ResponseEntity<?> register(@RequestBody RegisterRequest req) {
|
||||
User user = userService.register(req.getUsername(), req.getEmail(), req.getPassword());
|
||||
emailService.sendEmail(user.getEmail(), "Welcome to OpenIsle", "Thank you for registering.");
|
||||
String token = jwtService.generateToken(user.getUsername());
|
||||
return ResponseEntity.ok(new JwtResponse(token));
|
||||
emailService.sendEmail(user.getEmail(), "Verification Code", "Your verification code is " + user.getVerificationCode());
|
||||
return ResponseEntity.ok(Map.of("message", "Verification code sent"));
|
||||
}
|
||||
|
||||
@PostMapping("/verify")
|
||||
public ResponseEntity<?> verify(@RequestBody VerifyRequest req) {
|
||||
boolean ok = userService.verifyCode(req.getUsername(), req.getCode());
|
||||
if (ok) {
|
||||
return ResponseEntity.ok(Map.of("message", "Verified"));
|
||||
}
|
||||
return ResponseEntity.badRequest().body(Map.of("error", "Invalid verification code"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,7 +48,7 @@ public class AuthController {
|
||||
public ResponseEntity<?> login(@RequestBody LoginRequest req) {
|
||||
return userService.authenticate(req.getUsername(), req.getPassword())
|
||||
.map(user -> ResponseEntity.ok(new JwtResponse(jwtService.generateToken(user.getUsername()))))
|
||||
.orElse(ResponseEntity.status(401).build());
|
||||
.orElse(ResponseEntity.status(401).body(Map.of("error", "Invalid credentials or user not verified")));
|
||||
}
|
||||
|
||||
@Data
|
||||
@@ -55,6 +64,12 @@ public class AuthController {
|
||||
private String password;
|
||||
}
|
||||
|
||||
@Data
|
||||
private static class VerifyRequest {
|
||||
private String username;
|
||||
private String code;
|
||||
}
|
||||
|
||||
@Data
|
||||
private static class JwtResponse {
|
||||
private final String token;
|
||||
|
||||
Reference in New Issue
Block a user