Add authentication middleware

This commit is contained in:
Achim Rohn
2025-09-17 17:32:50 +02:00
parent 8bc7b1e27b
commit 667345bc9e
5 changed files with 291 additions and 12 deletions
+19 -2
View File
@@ -2,20 +2,35 @@ package routes
import (
. "ersteller-lib"
"ersteller-lib/authentication"
"ersteller-lib/starter/about"
"ersteller-lib/starter/contact"
"ersteller-lib/starter/ent"
"ersteller-lib/starter/env"
"ersteller-lib/starter/google"
"ersteller-lib/starter/index"
"ersteller-lib/starter/page"
"net/http"
"github.com/gorilla/sessions"
. "maragu.dev/gomponents"
)
func CreateApi() http.Handler {
func CreateApi(environment env.Environment, db *ent.Client) http.Handler {
server := NewHtmxServer()
HtmxRouteDebugTrace = true
sessionStore := sessions.NewCookieStore([]byte(environment.SessionSecret))
//sessionStore.Options.Secure = false
if !environment.IsDev {
sessionStore.Options.Secure = true
sessionStore.Options.HttpOnly = true
}
googleAuth := google.NewGoogleAuth(db, server.GetHttpServer(), environment, sessionStore)
googleAuth.AddRoutes()
indexActivePath := NewActivePath(map[Language]string{
En: "Home",
De: "Startseite",
@@ -54,7 +69,9 @@ func CreateApi() http.Handler {
_ = about.NewPage(createPageFunc, server, &aboutActivePath)
_ = contact.NewPage(createPageFunc, server, &contactActivePath)
serverWithMiddleWare := UseMiddleware(server, LoggingMiddleware, MakeGzipHandler)
serverWithMiddleWare := UseMiddleware(server, LoggingMiddleware, MakeGzipHandler,
authentication.Middleware(sessionStore,
[]string{"/login", google.GoogleLogin, google.GoogleLoginCallback}, "/"))
return serverWithMiddleWare
}