feat: 设置页面修改用户名,需要重新发放token

This commit is contained in:
Tim
2025-07-29 19:12:39 +08:00
parent 9aec1afcf5
commit 3dbeb25a09
3 changed files with 24 additions and 8 deletions

View File

@@ -28,6 +28,7 @@ public class UserController {
private final PostReadService postReadService;
private final UserVisitService userVisitService;
private final LevelService levelService;
private final JwtService jwtService;
@Value("${app.upload.check-type:true}")
private boolean checkImageType;
@@ -73,10 +74,13 @@ public class UserController {
}
@PutMapping("/me")
public ResponseEntity<UserDto> updateProfile(@RequestBody UpdateProfileDto dto,
public ResponseEntity<?> updateProfile(@RequestBody UpdateProfileDto dto,
Authentication auth) {
User user = userService.updateProfile(auth.getName(), dto.getUsername(), dto.getIntroduction());
return ResponseEntity.ok(toDto(user, auth));
return ResponseEntity.ok(Map.of(
"token", jwtService.generateToken(user.getUsername()),
"user", toDto(user, auth)
));
}
@PostMapping("/me/signin")

View File

@@ -122,9 +122,13 @@ public class SubscriptionService {
if (subscriberName == null || targetName == null || subscriberName.equals(targetName)) {
return false;
}
User subscriber = userRepo.findByUsername(subscriberName).orElseThrow();
User target = findUser(targetName).orElseThrow();
return userSubRepo.findBySubscriberAndTarget(subscriber, target).isPresent();
Optional<User> subscriber = userRepo.findByUsername(subscriberName);
Optional<User> target = findUser(targetName);
if (subscriber.isEmpty() || target.isEmpty()) {
// 修改个人信息会出现,先不抛出错误
return false;
}
return userSubRepo.findBySubscriberAndTarget(subscriber.get(), target.get()).isPresent();
}
public boolean isPostSubscribed(String username, Long postId) {