moved auth into middleware

This commit is contained in:
2026-01-23 23:34:22 -07:00
parent 649595718c
commit e4069e18e2
6 changed files with 103 additions and 129 deletions
+30 -4
View File
@@ -79,7 +79,11 @@ func NewRouter(
// kind of a dumb way to capture routes for webpages
templates_api.SetupRoutes(
logger.WithGroup("templates"),
r.Group("/ui"),
r.Group(
"/ui",
stripPrefix("/ui"),
authMiddleware.AddIdentityToRequest,
),
contentDir,
rawEvents,
accts,
@@ -107,13 +111,20 @@ func NewRouter(
auth,
)
sse_api.Routes(
api.Group("/events"),
api.Group(
"/events",
authMiddleware.AddIdentityToRequest,
response.Handler(authMiddleware.Authenticate()),
),
apiLogger.WithGroup("/events"),
sq,
authMiddleware,
)
accounts_api.Routes(
api.Group("/accounts", authMiddleware.AuthenticateAndAddIdentityGin()),
api.Group(
"/accounts",
authMiddleware.AddIdentityToRequest,
response.Handler(authMiddleware.Authenticate()),
),
apiLogger.WithGroup("/accounts"),
accts,
unp.Group("/accounts"),
@@ -158,3 +169,18 @@ func fileServer(urlPrefix, dir string, beforeServe func(c *gin.Context)) gin.Han
// TODO: need a c.Next()?
}
}
func stripPrefix(prefix string) gin.HandlerFunc {
return func(c *gin.Context) {
if !strings.HasPrefix(c.Request.URL.Path, prefix) {
return
}
c.Request.URL.Path = c.Request.URL.Path[len(prefix):]
defer func() {
c.Request.URL.Path = prefix + c.Request.URL.Path
}()
c.Next()
}
}