mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-03-18 18:07:24 +08:00
feat: update config
This commit is contained in:
@@ -9,6 +9,20 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
/*
|
||||||
|
curl -X POST http://localhost:8080/api/auth/register \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{
|
||||||
|
"username": "test_user",
|
||||||
|
"email": "cjt807916@gmail.com",
|
||||||
|
"password": "password"
|
||||||
|
}'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/auth")
|
@RequestMapping("/api/auth")
|
||||||
@@ -18,11 +32,6 @@ public class AuthController {
|
|||||||
private final JwtService jwtService;
|
private final JwtService jwtService;
|
||||||
private final EmailService emailService;
|
private final EmailService emailService;
|
||||||
|
|
||||||
/**
|
|
||||||
* curl -X POST http://localhost:8080/api/auth/login \
|
|
||||||
* -H "Content-Type: application/json" \
|
|
||||||
* -d '{"username":"test","password":"password"}'
|
|
||||||
*/
|
|
||||||
@PostMapping("/register")
|
@PostMapping("/register")
|
||||||
public ResponseEntity<?> register(@RequestBody RegisterRequest req) {
|
public ResponseEntity<?> register(@RequestBody RegisterRequest req) {
|
||||||
User user = userService.register(req.getUsername(), req.getEmail(), req.getPassword());
|
User user = userService.register(req.getUsername(), req.getEmail(), req.getPassword());
|
||||||
@@ -39,16 +48,14 @@ public class AuthController {
|
|||||||
return ResponseEntity.badRequest().body(Map.of("error", "Invalid verification code"));
|
return ResponseEntity.badRequest().body(Map.of("error", "Invalid verification code"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* curl -X POST http://localhost:8080/api/auth/login \
|
|
||||||
* -H "Content-Type: application/json" \
|
|
||||||
* -d '{"username":"test","password":"password"}'
|
|
||||||
*/
|
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
public ResponseEntity<?> login(@RequestBody LoginRequest req) {
|
public ResponseEntity<?> login(@RequestBody LoginRequest req) {
|
||||||
return userService.authenticate(req.getUsername(), req.getPassword())
|
Optional<User> user = userService.authenticate(req.getUsername(), req.getPassword());
|
||||||
.map(user -> ResponseEntity.ok(new JwtResponse(jwtService.generateToken(user.getUsername()))))
|
if (user.isPresent()) {
|
||||||
.orElse(ResponseEntity.status(401).body(Map.of("error", "Invalid credentials or user not verified")));
|
return ResponseEntity.ok(Map.of("token", jwtService.generateToken(user.get().getUsername())));
|
||||||
|
} else {
|
||||||
|
return ResponseEntity.badRequest().body(Map.of("error", "\"Invalid credentials or user not verified"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@@ -69,9 +76,4 @@ public class AuthController {
|
|||||||
private String username;
|
private String username;
|
||||||
private String code;
|
private String code;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
|
||||||
private static class JwtResponse {
|
|
||||||
private final String token;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ public class EmailService {
|
|||||||
body.put("to", to);
|
body.put("to", to);
|
||||||
body.put("subject", subject);
|
body.put("subject", subject);
|
||||||
body.put("text", text);
|
body.put("text", text);
|
||||||
body.put("from", "cjt807916@gmail.com"); // todo(tim): use config
|
body.put("from", "openisle <noreply@chenjiating.com>"); // todo(tim): use config
|
||||||
|
|
||||||
HttpEntity<Map<String, String>> entity = new HttpEntity<>(body, headers);
|
HttpEntity<Map<String, String>> entity = new HttpEntity<>(body, headers);
|
||||||
restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
|
restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
|
||||||
|
|||||||
Reference in New Issue
Block a user