start setting up auth handler

This commit is contained in:
juancwu 2025-12-15 21:50:54 -05:00
commit 979a415b95
4 changed files with 62 additions and 0 deletions

View file

@ -0,0 +1,24 @@
package middleware
import (
"net/http"
"git.juancwu.dev/juancwu/budgething/internal/ctxkeys"
)
// RequireGuest ensures request is not authenticated
func RequireGuest(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
user := ctxkeys.User(r.Context())
if user != nil {
if r.Header.Get("HX-Request") == "true" {
w.Header().Set("HX-Redirect", "/app/dashboard")
w.WriteHeader(http.StatusSeeOther)
return
}
http.Redirect(w, r, "/app/dashboard", http.StatusSeeOther)
return
}
next.ServeHTTP(w, r)
}
}