Skip to content

Commit 2aa2910

Browse files
committed
fix: clean up WebAuthn credentials when Test API deletes a user
Deleting a user with registered passkeys failed on the user_entities foreign key. Publish UserPreDeleteEvent so the framework's WebAuthnPreDeleteEventListener removes the credentials and user entity first, same as the normal deletion flow.
1 parent 48652c4 commit 2aa2910

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

src/main/java/com/digitalsanctuary/spring/demo/test/api/TestDataController.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Date;
77
import java.util.HashMap;
88
import java.util.Map;
9+
import org.springframework.context.ApplicationEventPublisher;
910
import org.springframework.context.annotation.Profile;
1011
import org.springframework.http.HttpStatus;
1112
import org.springframework.http.ResponseEntity;
@@ -19,6 +20,7 @@
1920
import org.springframework.web.bind.annotation.RequestParam;
2021
import org.springframework.web.bind.annotation.RestController;
2122
import com.digitalsanctuary.spring.demo.user.profile.DemoUserProfileRepository;
23+
import com.digitalsanctuary.spring.user.event.UserPreDeleteEvent;
2224
import com.digitalsanctuary.spring.user.persistence.model.PasswordResetToken;
2325
import com.digitalsanctuary.spring.user.persistence.model.Role;
2426
import com.digitalsanctuary.spring.user.persistence.model.User;
@@ -50,6 +52,7 @@ public class TestDataController {
5052
private final RoleRepository roleRepository;
5153
private final PasswordEncoder passwordEncoder;
5254
private final DemoUserProfileRepository demoUserProfileRepository;
55+
private final ApplicationEventPublisher eventPublisher;
5356

5457
/**
5558
* Check if a user exists by email.
@@ -231,6 +234,10 @@ public ResponseEntity<Map<String, Object>> deleteTestUser(@RequestParam String e
231234
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
232235
}
233236

237+
// Let framework listeners clean up their data first (e.g. WebAuthn credentials and user
238+
// entities, which have a foreign key on the user account)
239+
eventPublisher.publishEvent(new UserPreDeleteEvent(this, user));
240+
234241
// Delete related entities first to avoid foreign key constraints
235242
demoUserProfileRepository.findById(user.getId()).ifPresent(demoUserProfileRepository::delete);
236243

0 commit comments

Comments
 (0)