Files
warmbly/internal/api/handler/auth.go
T
Matthew Meszaros c8c4440b50 fix: confirm dialog z + theme, persist folders/tags across reload
User: "when I click on delete the confirm appears behind the form and
it looks really bad, doesn't fit in the theme; and also after I reload
the page, nothing appears after creation".

Two distinct bugs:

1) Confirm dialog stacking + styling
   FoldersModal/TagsModal render at z-[110]. ConfirmProvider rendered
   the confirm overlay at z-101 with bg-black/30 + scale animation +
   poppins styling — visually it landed BEHIND the folders modal and
   clicks went through to the backdrop instead.
   Rewrote ConfirmProvider in the brae chrome:
   - z-[200] so it stacks above page-level overlays AND nested
     dialogs.
   - Hairline-bordered card, 48px header (red alert tile + "Confirm"
     eyebrow), prose body, slate-900 footer (Cancel / red Confirm).
   - Escape closes; backdrop closes (both gated on !loading).
   - Spinner inside Confirm during the awaited action.

2) Created folders/tags disappeared after page reload
   POST /folders + /tags persisted to Postgres fine. The frontend
   optimistic-updated the cached user via setQueryData. But
   /auth/me did not return folders/tags/categories — the User payload
   omitted them entirely. On reload the cache refetched /auth/me,
   got missing fields, defaulted to [], and the items vanished from
   the UI.

   Backend fix:
   - models.User now carries Folders/Tags/Categories ([]Group),
     always serialized as arrays.
   - GroupRepository + GroupService gained a List(ctx, userID)
     method; ordered by position then created_at.
   - /auth/me handler now calls List on FolderService, TagService,
     CategoryService and attaches them to the user before responding.

Verified end-to-end:
  GET /auth/me → 200 with full folders/tags arrays populated.
  Create a folder, reload the page → folder still in the list.
2026-05-23 09:38:20 +00:00

200 lines
4.6 KiB
Go

package handler
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/warmbly/warmbly/internal/api/middleware"
"github.com/warmbly/warmbly/internal/app/audit"
"github.com/warmbly/warmbly/internal/app/auth"
"github.com/warmbly/warmbly/internal/errx"
)
func (h *Handler) LoginStart(c *gin.Context) {
var data auth.AuthData
if err := c.ShouldBindJSON(&data); err != nil {
errx.Handle(c, errx.ErrInvalid)
return
}
resp, err := h.AuthService.LoginStart(c.Request.Context(), &data, c.ClientIP())
if err != nil {
errx.Handle(c, err)
return
}
c.JSON(http.StatusOK, resp)
}
func (h *Handler) LoginConfirm(c *gin.Context) {
var data auth.ConfirmData
if err := c.ShouldBindJSON(&data); err != nil {
errx.Handle(c, err)
return
}
resp, err := h.AuthService.LoginConfirm(c.Request.Context(), &data, data.Session, c.ClientIP(), c.Request.UserAgent())
if err != nil {
errx.Handle(c, err)
return
}
c.JSON(http.StatusOK, resp)
}
func (h *Handler) RegistrationStart(c *gin.Context) {
var data auth.AuthData
if err := c.ShouldBindJSON(&data); err != nil {
errx.Handle(c, errx.ErrInvalid)
return
}
resp, err := h.AuthService.RegistrationStart(c.Request.Context(), &data, c.ClientIP())
if err != nil {
errx.Handle(c, err)
return
}
c.JSON(http.StatusOK, resp)
}
func (h *Handler) RegistrationConfirm(c *gin.Context) {
var data auth.ConfirmData
if err := c.ShouldBindJSON(&data); err != nil {
errx.Handle(c, err)
return
}
if err := h.AuthService.RegistrationConfirm(c.Request.Context(), &data, data.Session, c.ClientIP()); err != nil {
errx.Handle(c, err)
return
}
c.Status(http.StatusNoContent)
}
func (h *Handler) RefreshToken(c *gin.Context) {
var data struct {
RefreshToken string `json:"refresh_token"`
}
if err := c.ShouldBindJSON(&data); err != nil {
errx.Handle(c, errx.ErrInvalid)
return
}
token, err := h.TokenService.RefreshToken(c.Request.Context(), data.RefreshToken)
if err != nil {
errx.Handle(c, err)
return
}
c.JSON(http.StatusOK, token)
}
func (h *Handler) Logout(c *gin.Context) {
accessToken := middleware.GetAccessToken(c)
userIDStr := middleware.GetUserID(c)
if err := h.TokenService.RevokeSession(c.Request.Context(), accessToken); err != nil {
errx.Handle(c, err)
return
}
// Audit log
if userID, err := uuid.Parse(userIDStr); err == nil {
audit.LogLogout(h.AuditService, c.Request.Context(), userID, c.ClientIP(), c.Request.UserAgent())
}
c.Status(http.StatusNoContent)
}
func (h *Handler) LogoutAll(c *gin.Context) {
accessToken := middleware.GetAccessToken(c)
userIDStr := middleware.GetUserID(c)
if err := h.TokenService.RevokeAllSession(c.Request.Context(), accessToken); err != nil {
errx.Handle(c, err)
return
}
// Audit log
if userID, err := uuid.Parse(userIDStr); err == nil {
audit.LogLogout(h.AuditService, c.Request.Context(), userID, c.ClientIP(), c.Request.UserAgent())
}
c.Status(http.StatusNoContent)
}
func (h *Handler) GetUser(c *gin.Context) {
userID := middleware.GetUserID(c)
uid, err := uuid.Parse(userID)
if err != nil {
errx.Handle(c, errx.ErrUser)
return
}
ctx := c.Request.Context()
u, xerr := h.UserService.GetUser(ctx, uid)
if xerr != nil {
errx.Handle(c, xerr)
return
}
// Populate the per-user label groups so the frontend can render
// folder/tag pickers on initial page load without three extra
// round-trips. Without this, anything the user created in a
// previous session would disappear after a refresh: the cache
// would optimistic-update from a Create response, but on reload
// the /auth/me payload had empty folders/tags/categories.
if folders, ferr := h.FolderService.List(ctx, uid); ferr == nil {
u.Folders = folders
}
if tags, terr := h.TagService.List(ctx, uid); terr == nil {
u.Tags = tags
}
if cats, cerr := h.CategoryService.List(ctx, uid); cerr == nil {
u.Categories = cats
}
c.JSON(http.StatusOK, u)
}
func (h *Handler) ResetPasswordStart(c *gin.Context) {
var data auth.ResetPasswordStart
if err := c.ShouldBindJSON(&data); err != nil {
errx.Handle(c, errx.ErrInvalid)
return
}
if err := h.AuthService.ResetPasswordStart(c.Request.Context(), &data, c.ClientIP()); err != nil {
errx.Handle(c, err)
return
}
c.Status(http.StatusOK)
}
func (h *Handler) ResetPasswordConfirm(c *gin.Context) {
var data auth.ResetPasswordConfirm
if err := c.ShouldBindJSON(&data); err != nil {
errx.Handle(c, errx.ErrInvalid)
return
}
if err := h.AuthService.ResetPasswordConfirm(c.Request.Context(), &data, data.Session, c.ClientIP()); err != nil {
errx.Handle(c, err)
return
}
c.Status(http.StatusOK)
}