package auth import ( "errors" "net/http" "github.com/gin-gonic/gin" "backend/internal/modules/users" "backend/internal/platform/middleware" "backend/internal/platform/security" ) // customer cookie name/path are intentionally distinct from the admin ones // (see admin_handler.go) so the two spaces never share a session cookie. const ( customerRefreshCookieName = "customer_refresh_token" customerRefreshCookiePath = "/api/auth/customer" ) // CustomerHandler mirrors AdminHandler but issues/accepts only // customer-audience tokens. Not mounted by main.go in this phase (no // customer-facing module needs it yet) — it exists so the future // cart/orders module can activate customer auth without touching the admin // code path at all. type CustomerHandler struct { service *Service cookieSecure bool } func NewCustomerHandler(service *Service, cookieSecure bool) *CustomerHandler { return &CustomerHandler{service: service, cookieSecure: cookieSecure} } func (h *CustomerHandler) Login(c *gin.Context) { var req loginRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "validation error", "details": err.Error()}) return } pair, user, err := h.service.Login(c.Request.Context(), security.AudienceCustomer, users.RoleCustomer, req.Email, req.Password) if err != nil { switch { case errors.Is(err, ErrInvalidCredentials), errors.Is(err, ErrAccountDisabled): c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid credentials"}) default: c.JSON(http.StatusInternalServerError, gin.H{"error": "login failed"}) } return } h.setRefreshCookie(c, pair.RefreshToken) c.JSON(http.StatusOK, accessTokenResponse{ AccessToken: pair.AccessToken, User: &authUserResponse{ID: user.ID, Email: user.Email, Role: user.Role}, }) } func (h *CustomerHandler) Refresh(c *gin.Context) { token, err := c.Cookie(customerRefreshCookieName) if err != nil || token == "" { c.JSON(http.StatusUnauthorized, gin.H{"error": "missing refresh token"}) return } pair, err := h.service.Refresh(c.Request.Context(), security.AudienceCustomer, token) if err != nil { h.clearRefreshCookie(c) c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid or expired refresh token"}) return } h.setRefreshCookie(c, pair.RefreshToken) c.JSON(http.StatusOK, accessTokenResponse{AccessToken: pair.AccessToken}) } func (h *CustomerHandler) Logout(c *gin.Context) { if token, err := c.Cookie(customerRefreshCookieName); err == nil && token != "" { _ = h.service.Logout(c.Request.Context(), security.AudienceCustomer, token) } h.clearRefreshCookie(c) c.JSON(http.StatusOK, gin.H{"message": "logged out"}) } func (h *CustomerHandler) Me(c *gin.Context) { userID, ok := middleware.GetUserID(c) if !ok { c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"}) return } user, err := h.service.Me(c.Request.Context(), userID) if err != nil { if errors.Is(err, users.ErrNotFound) { c.JSON(http.StatusNotFound, gin.H{"error": "user not found"}) return } c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to load user"}) return } c.JSON(http.StatusOK, authUserResponse{ID: user.ID, Email: user.Email, Role: user.Role}) } func (h *CustomerHandler) setRefreshCookie(c *gin.Context, token string) { c.SetSameSite(http.SameSiteStrictMode) c.SetCookie(customerRefreshCookieName, token, int(h.service.RefreshTTL().Seconds()), customerRefreshCookiePath, "", h.cookieSecure, true) } func (h *CustomerHandler) clearRefreshCookie(c *gin.Context) { c.SetSameSite(http.SameSiteStrictMode) c.SetCookie(customerRefreshCookieName, "", -1, customerRefreshCookiePath, "", h.cookieSecure, true) }