Skip to content

Commit 4fc3d90

Browse files
appleboyclaude
andcommitted
refactor(bootstrap): build RequireAuth once and reuse across route groups
Cleanup follow-up. setupAllRoutes already constructs optionalAuth and injectPending once and reuses them; the auth-middleware threading added a fresh middleware.RequireAuth(h.userService, prometheusMetrics) at each of the 6 route groups. Build it once into a local and reuse, matching the surrounding pattern and collapsing the duplicated multi-line .Use() blocks. No behavior change. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 53e8483 commit 4fc3d90

1 file changed

Lines changed: 8 additions & 21 deletions

File tree

internal/bootstrap/router.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,10 @@ func setupAllRoutes(
193193
c.Redirect(http.StatusFound, "/account/sessions")
194194
})
195195

196-
// Documentation routes (public, optional auth for navbar)
196+
// Auth middlewares are stateless per request, so build each once and reuse
197+
// across route groups (mirrors optionalAuth / injectPending below).
197198
optionalAuth := middleware.OptionalAuth(h.userService, prometheusMetrics)
199+
requireAuth := middleware.RequireAuth(h.userService, prometheusMetrics)
198200
r.GET("/docs", optionalAuth, h.docs.ShowDocsIndex)
199201
// :lang is either a supported locale code (new canonical form) or, for
200202
// backwards-compatibility, a legacy slug; ShowDocsEntry disambiguates.
@@ -268,10 +270,7 @@ func setupAllRoutes(
268270

269271
// OAuth Authorization Code Flow (browser, requires login + CSRF)
270272
oauthProtected := r.Group("/oauth")
271-
oauthProtected.Use(
272-
middleware.RequireAuth(h.userService, prometheusMetrics),
273-
middleware.CSRFMiddleware(),
274-
)
273+
oauthProtected.Use(requireAuth, middleware.CSRFMiddleware())
275274
{
276275
oauthProtected.GET("/authorize", h.authorization.ShowAuthorizePage)
277276
oauthProtected.POST("/authorize", h.authorization.HandleAuthorize)
@@ -283,23 +282,15 @@ func setupAllRoutes(
283282

284283
// Protected routes (require login)
285284
protected := r.Group("")
286-
protected.Use(
287-
middleware.RequireAuth(h.userService, prometheusMetrics),
288-
middleware.CSRFMiddleware(),
289-
injectPending,
290-
)
285+
protected.Use(requireAuth, middleware.CSRFMiddleware(), injectPending)
291286
{
292287
protected.GET("/device", h.device.DevicePage)
293288
protected.POST("/device/verify", rateLimiters.deviceVerify, h.device.DeviceVerify)
294289
}
295290

296291
// Account routes (require login)
297292
account := r.Group("/account")
298-
account.Use(
299-
middleware.RequireAuth(h.userService, prometheusMetrics),
300-
middleware.CSRFMiddleware(),
301-
injectPending,
302-
)
293+
account.Use(requireAuth, middleware.CSRFMiddleware(), injectPending)
303294
{
304295
account.GET("/sessions", h.session.ListSessions)
305296
account.POST("/sessions/:id/revoke", h.session.RevokeSession)
@@ -313,11 +304,7 @@ func setupAllRoutes(
313304

314305
// User apps area (all authenticated users, not admin-only)
315306
apps := r.Group("/apps")
316-
apps.Use(
317-
middleware.RequireAuth(h.userService, prometheusMetrics),
318-
middleware.CSRFMiddleware(),
319-
injectPending,
320-
)
307+
apps.Use(requireAuth, middleware.CSRFMiddleware(), injectPending)
321308
{
322309
apps.GET("", h.userClient.ShowMyAppsPage)
323310
apps.GET("/new", h.userClient.ShowCreateAppPage)
@@ -333,7 +320,7 @@ func setupAllRoutes(
333320
// Admin routes (require admin role)
334321
admin := r.Group("/admin")
335322
admin.Use(
336-
middleware.RequireAuth(h.userService, prometheusMetrics),
323+
requireAuth,
337324
middleware.RequireAdmin(),
338325
middleware.CSRFMiddleware(),
339326
injectPending,

0 commit comments

Comments
 (0)