forked from RyanGoodwill/backend
优化失败message展示逻辑
This commit is contained in:
parent
80ae2fb8d3
commit
4eb09c3bca
@ -27,12 +27,17 @@ class AuthenticationController {
|
||||
|
||||
@PostMapping("/register")
|
||||
public ResponseEntity<RegisterResponse> register(@RequestBody RegisterRequest request) {
|
||||
boolean isRegistered = userService.registerUser(request.getUsername(), request.getPassword(), request.getContactInfo());
|
||||
if (isRegistered) {
|
||||
return new ResponseEntity<>(new RegisterResponse("Registration successful!"), HttpStatus.OK);
|
||||
} else {
|
||||
return new ResponseEntity<>(new RegisterResponse("Registration failed! User already exist"), HttpStatus.BAD_REQUEST);
|
||||
try {
|
||||
boolean isRegistered = userService.registerUser(request.getUsername(), request.getPassword(), request.getContactInfo());
|
||||
if (isRegistered) {
|
||||
return new ResponseEntity<>(new RegisterResponse("注册成功"), HttpStatus.OK);
|
||||
} else {
|
||||
return new ResponseEntity<>(new RegisterResponse("注册失败:用户已存在"), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(new RegisterResponse("注册失败:输入格式有误"), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -15,7 +15,7 @@ public class UserService {
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||
private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||
|
||||
public UserHS getUserByUsername(String username) {
|
||||
return userRepository.findByUsername(username);
|
||||
@ -29,7 +29,7 @@ public class UserService {
|
||||
|
||||
// 验证输入格式
|
||||
if (!isValidUsername(username) || !isValidPassword(password) || !isValidContactInfo(contactInfo)) {
|
||||
return false; // 输入格式不正确
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
// 加密密码
|
||||
|
||||
Loading…
Reference in New Issue
Block a user