Files
main-tbbank/backend/internal/admin/ui/handler.go
GuwandykA 5f2e2f44ca Fix admin login reload loop from stale localStorage tokens.
Clear client tokens on the login page, gate admin pages with shared cookie JWT checks, and clear Secure cookies reliably behind the proxy.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-07 11:20:22 +05:00

109 lines
2.7 KiB
Go

// Package ui serves the admin dashboard pages at /admin.
// Pages are template shells; all data is fetched client-side from the
// existing JWT-protected /api/v1/admin API.
package ui
import (
"bankline/internal/appresult"
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
// Resource describes an admin CRUD resource rendered by the generic
// list/form templates. Slug is both the URL segment and the key used by
// the client-side schema in resources.js.
type Resource struct {
Slug string
Title string
}
var resources = []Resource{
{Slug: "categories", Title: "Categories"},
{Slug: "sub-categories", Title: "Sub-categories"},
{Slug: "banners", Title: "Banners"},
{Slug: "news", Title: "News"},
{Slug: "static", Title: "Static pages"},
{Slug: "currency", Title: "Currency rates"},
}
// Register mounts templates, static assets and all dashboard page routes.
func Register(router *gin.Engine) {
router.LoadHTMLGlob("web/admin/templates/**/*.html")
router.Static("/admin/assets", "./web/admin/assets")
admin := router.Group("/admin")
admin.GET("/login", appresult.AdminGuestOnly(), func(c *gin.Context) {
c.HTML(http.StatusOK, "pages/login", gin.H{
"Title": "Login",
"Page": "login",
})
})
authed := admin.Group("")
authed.Use(appresult.AdminPageAuth())
authed.GET("", func(c *gin.Context) {
c.HTML(http.StatusOK, "pages/dashboard", gin.H{
"Title": "Dashboard",
"Page": "dashboard",
"Active": "dashboard",
})
})
authed.GET("/settings", func(c *gin.Context) {
c.HTML(http.StatusOK, "pages/settings", gin.H{
"Title": "Settings",
"Page": "settings",
"Active": "settings",
})
})
for _, res := range resources {
res := res
authed.GET("/"+res.Slug, func(c *gin.Context) {
c.HTML(http.StatusOK, "pages/list", gin.H{
"Title": res.Title,
"Page": "list",
"Active": res.Slug,
"Resource": res.Slug,
})
})
authed.GET("/"+res.Slug+"/new", func(c *gin.Context) {
c.HTML(http.StatusOK, "pages/form", gin.H{
"Title": "New — " + res.Title,
"Page": "form",
"Active": res.Slug,
"Resource": res.Slug,
"Mode": "create",
})
})
authed.GET("/"+res.Slug+"/:id/edit", func(c *gin.Context) {
c.HTML(http.StatusOK, "pages/form", gin.H{
"Title": "Edit — " + res.Title,
"Page": "form",
"Active": res.Slug,
"Resource": res.Slug,
"Mode": "edit",
"Id": c.Param("id"),
})
})
}
router.NoRoute(func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/admin") {
c.HTML(http.StatusNotFound, "pages/404", gin.H{
"Title": "Page not found",
"Page": "404",
})
return
}
c.JSON(http.StatusNotFound, gin.H{"status": false, "message": "not found"})
})
}