Conditionally render admin basic
This commit is contained in:
+33
-21
@@ -43,6 +43,14 @@ func SumUniquePostIps(posts []database.Post) int {
|
|||||||
return len(uniqueIpHashes)
|
return len(uniqueIpHashes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isAdmin(r *http.Request) bool {
|
||||||
|
isAdmin := false
|
||||||
|
if c, err := r.Cookie("comfy_admin"); err == nil {
|
||||||
|
isAdmin = util.IsAdminSessionValid(c.Value)
|
||||||
|
}
|
||||||
|
return isAdmin
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// -----------------
|
// -----------------
|
||||||
// SETUP
|
// SETUP
|
||||||
@@ -70,7 +78,7 @@ func main() {
|
|||||||
// -----------------
|
// -----------------
|
||||||
|
|
||||||
// INDEX PAGE
|
// INDEX PAGE
|
||||||
r.With(util.AdminOnlyMiddleware).Get("/", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
views.Index().Render(r.Context(), w)
|
views.Index().Render(r.Context(), w)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -85,7 +93,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
views.Board(board).Render(r.Context(), w)
|
views.Board(board, isAdmin(r)).Render(r.Context(), w)
|
||||||
})
|
})
|
||||||
|
|
||||||
// THREAD PAGE
|
// THREAD PAGE
|
||||||
@@ -365,7 +373,7 @@ func main() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
views.ThreadsCatalog(previews).Render(r.Context(), w)
|
views.ThreadsCatalog(previews, isAdmin(r)).Render(r.Context(), w)
|
||||||
})
|
})
|
||||||
|
|
||||||
// THREAD POSTS
|
// THREAD POSTS
|
||||||
@@ -408,12 +416,12 @@ func main() {
|
|||||||
// ADMIN ROUTES (htmx)
|
// ADMIN ROUTES (htmx)
|
||||||
// -----------------
|
// -----------------
|
||||||
|
|
||||||
r.Get("/admin/login", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/login", func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Println(util.AdminSessions)
|
fmt.Println(util.AdminSessions)
|
||||||
admin.AdminLogin().Render(r.Context(), w)
|
admin.AdminLogin().Render(r.Context(), w)
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Post("/admin/login", func(w http.ResponseWriter, r *http.Request) {
|
r.Post("/login", func(w http.ResponseWriter, r *http.Request) {
|
||||||
username := r.FormValue("username")
|
username := r.FormValue("username")
|
||||||
password := r.FormValue("password")
|
password := r.FormValue("password")
|
||||||
|
|
||||||
@@ -453,22 +461,7 @@ func main() {
|
|||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Post("/admin/logout", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/authorized", func(w http.ResponseWriter, r *http.Request) {
|
||||||
if c, err := r.Cookie("comfy_admin"); err == nil {
|
|
||||||
util.DeleteAdminSession(c.Value)
|
|
||||||
}
|
|
||||||
http.SetCookie(w, &http.Cookie{
|
|
||||||
Name: "comfy_admin",
|
|
||||||
Value: "",
|
|
||||||
HttpOnly: true,
|
|
||||||
Secure: !dev,
|
|
||||||
Expires: time.Now(),
|
|
||||||
SameSite: http.SameSiteStrictMode,
|
|
||||||
Path: "/",
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
r.Get("/admin/me", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||||
|
|
||||||
if c, err := r.Cookie("comfy_admin"); err == nil && util.IsAdminSessionValid(c.Value) {
|
if c, err := r.Cookie("comfy_admin"); err == nil && util.IsAdminSessionValid(c.Value) {
|
||||||
@@ -478,6 +471,25 @@ func main() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
r.Route("/admin", func(r chi.Router) {
|
||||||
|
r.Use(util.AdminOnlyMiddleware)
|
||||||
|
|
||||||
|
r.Post("/logout", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if c, err := r.Cookie("comfy_admin"); err == nil {
|
||||||
|
util.DeleteAdminSession(c.Value)
|
||||||
|
}
|
||||||
|
http.SetCookie(w, &http.Cookie{
|
||||||
|
Name: "comfy_admin",
|
||||||
|
Value: "",
|
||||||
|
HttpOnly: true,
|
||||||
|
Secure: !dev,
|
||||||
|
Expires: time.Now(),
|
||||||
|
SameSite: http.SameSiteStrictMode,
|
||||||
|
Path: "/",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
// -----------------
|
// -----------------
|
||||||
|
|
||||||
// -----------------
|
// -----------------
|
||||||
|
|||||||
@@ -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="/admin/login"
|
hx-post="/login"
|
||||||
hx-swap="none"
|
hx-swap="none"
|
||||||
id="adminLoginForm"
|
id="adminLoginForm"
|
||||||
_="
|
_="
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ templ BoardHeader(board database.Board) {
|
|||||||
</header>
|
</header>
|
||||||
}
|
}
|
||||||
|
|
||||||
templ Board(board database.Board) {
|
templ Board(board database.Board, isAdmin bool) {
|
||||||
@shared.Layout() {
|
@shared.Layout() {
|
||||||
@BoardHeader(board)
|
@BoardHeader(board)
|
||||||
<div class="new-post-container">
|
<div class="new-post-container">
|
||||||
@@ -58,7 +58,7 @@ type CatalogThreadPreview struct {
|
|||||||
IpCount int
|
IpCount int
|
||||||
}
|
}
|
||||||
|
|
||||||
templ ThreadsCatalog(previews []CatalogThreadPreview) {
|
templ ThreadsCatalog(previews []CatalogThreadPreview, isAdmin bool) {
|
||||||
<div id="catalog">
|
<div id="catalog">
|
||||||
for _, preview := range previews {
|
for _, preview := range previews {
|
||||||
{{ replyCount := strconv.FormatInt(int64(preview.ReplyCount), 10) }}
|
{{ replyCount := strconv.FormatInt(int64(preview.ReplyCount), 10) }}
|
||||||
@@ -79,6 +79,9 @@ templ ThreadsCatalog(previews []CatalogThreadPreview) {
|
|||||||
<p>
|
<p>
|
||||||
@templ.Raw(util.EnrichPost(preview.Body))
|
@templ.Raw(util.EnrichPost(preview.Body))
|
||||||
</p>
|
</p>
|
||||||
|
if isAdmin {
|
||||||
|
<button>X</button>
|
||||||
|
}
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user