diff --git a/src/Heimdall.Web/Controllers/ControllerBaseExtensions.cs b/src/Heimdall.Web/Controllers/ControllerBaseExtensions.cs
new file mode 100644
index 0000000..9fcd48d
--- /dev/null
+++ b/src/Heimdall.Web/Controllers/ControllerBaseExtensions.cs
@@ -0,0 +1,21 @@
+using System.Security.Claims;
+using Microsoft.AspNetCore.Mvc;
+
+namespace Heimdall.Api.Controllers;
+
+///
+/// Extension methods for ControllerBase providing common user identity extraction
+/// from JWT claims, reducing boilerplate in API controllers.
+///
+public static class ControllerBaseExtensions
+{
+ ///
+ /// Tries to get the current user's GUID from the NameIdentifier claim.
+ /// Returns true and sets userId if the claim is valid.
+ ///
+ public static bool TryGetUserId(this ControllerBase controller, out Guid userId)
+ {
+ var claim = controller.User.FindFirstValue(ClaimTypes.NameIdentifier);
+ return Guid.TryParse(claim, out userId);
+ }
+}
diff --git a/src/Heimdall.Web/Controllers/SitesController.cs b/src/Heimdall.Web/Controllers/SitesController.cs
index ef4c6d9..4d5a1e4 100644
--- a/src/Heimdall.Web/Controllers/SitesController.cs
+++ b/src/Heimdall.Web/Controllers/SitesController.cs
@@ -32,8 +32,7 @@ public class SitesController : ControllerBase
public async Task Create(
[FromBody] CreateSiteRequest request, CancellationToken ct)
{
- var userIdClaim = User.FindFirstValue(ClaimTypes.NameIdentifier);
- if (string.IsNullOrEmpty(userIdClaim) || !Guid.TryParse(userIdClaim, out var userId))
+ if (!this.TryGetUserId(out _))
return Unauthorized(new { error = "Invalid user identity" });
var command = new CreateSiteCommand(request.Name, request.Address, request.Timezone);