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;
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.openisle.controller;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<?> handleException(Exception ex) {
|
||||
return ResponseEntity.badRequest().body(Map.of("error", ex.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.openisle.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
public class HelloController {
|
||||
@@ -10,7 +11,7 @@ public class HelloController {
|
||||
* -H "Authorization: Bearer <jwt-token>"
|
||||
*/
|
||||
@GetMapping("/api/hello")
|
||||
public String hello() {
|
||||
return "Hello, Authenticated User";
|
||||
public Map<String, String> hello() {
|
||||
return Map.of("message", "Hello, Authenticated User");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user