Small auth cleanup
This commit is contained in:
@@ -3,7 +3,6 @@ package util
|
|||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"net/http"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -66,14 +65,3 @@ func HasExistingAdminSession(username string) bool {
|
|||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func AdminOnlyMiddleware(next http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
c, err := r.Cookie("comfy_admin")
|
|
||||||
if err != nil || !IsAdminSessionValid(c.Value) {
|
|
||||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
next.ServeHTTP(w, r)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|||||||
+14
-4
@@ -25,6 +25,17 @@ import (
|
|||||||
|
|
||||||
var dev = true
|
var dev = true
|
||||||
|
|
||||||
|
func AdminOnlyMiddleware(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
c, err := r.Cookie("comfy_admin")
|
||||||
|
if err != nil || !util.IsAdminSessionValid(c.Value) {
|
||||||
|
http.Redirect(w, r, "/authorize", http.StatusSeeOther)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func disableCacheInDevMode(next http.Handler) http.Handler {
|
func disableCacheInDevMode(next http.Handler) http.Handler {
|
||||||
if !dev {
|
if !dev {
|
||||||
return next
|
return next
|
||||||
@@ -416,12 +427,11 @@ func main() {
|
|||||||
// ADMIN ROUTES (htmx)
|
// ADMIN ROUTES (htmx)
|
||||||
// -----------------
|
// -----------------
|
||||||
|
|
||||||
r.Get("/login", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/authorize", func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Println(util.AdminSessions)
|
|
||||||
admin.AdminLogin().Render(r.Context(), w)
|
admin.AdminLogin().Render(r.Context(), w)
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Post("/login", func(w http.ResponseWriter, r *http.Request) {
|
r.Post("/authorize", func(w http.ResponseWriter, r *http.Request) {
|
||||||
username := r.FormValue("username")
|
username := r.FormValue("username")
|
||||||
password := r.FormValue("password")
|
password := r.FormValue("password")
|
||||||
|
|
||||||
@@ -472,7 +482,7 @@ func main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
r.Route("/admin", func(r chi.Router) {
|
r.Route("/admin", func(r chi.Router) {
|
||||||
r.Use(util.AdminOnlyMiddleware)
|
r.Use(AdminOnlyMiddleware)
|
||||||
|
|
||||||
r.Post("/logout", func(w http.ResponseWriter, r *http.Request) {
|
r.Post("/logout", func(w http.ResponseWriter, r *http.Request) {
|
||||||
if c, err := r.Cookie("comfy_admin"); err == nil {
|
if c, err := r.Cookie("comfy_admin"); err == nil {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ templ AdminLogin() {
|
|||||||
<div class="admin-login-container">
|
<div class="admin-login-container">
|
||||||
<div style="display: none" id="adminLoginWarning" class="warning"></div>
|
<div style="display: none" id="adminLoginWarning" class="warning"></div>
|
||||||
<form
|
<form
|
||||||
hx-post="/login"
|
hx-post="/authorize"
|
||||||
hx-swap="none"
|
hx-swap="none"
|
||||||
id="adminLoginForm"
|
id="adminLoginForm"
|
||||||
_="
|
_="
|
||||||
|
|||||||
Reference in New Issue
Block a user