Files
geo/server/internal/tenant/transport/auth_handler.go
T
root 63667ed2d1 feat(membership): enforce tenant subscription plans with blocked UI
Add configurable membership plans (free/plus/pro) synced to DB on boot, a
subscription guard middleware that blocks tenant endpoints on expired or
missing plans, and a MembershipBlockedView that surfaces the reason so
the admin can contact the tenant owner. Quota and brand-library reads now
honor the active plan's policy JSON and expired subscriptions.
2026-04-18 20:56:05 +08:00

83 lines
1.9 KiB
Go

package transport
import (
"strings"
"github.com/gin-gonic/gin"
"github.com/geo-platform/tenant-api/internal/bootstrap"
"github.com/geo-platform/tenant-api/internal/shared/auth"
"github.com/geo-platform/tenant-api/internal/shared/response"
"github.com/geo-platform/tenant-api/internal/tenant/app"
"github.com/geo-platform/tenant-api/internal/tenant/repository"
)
type AuthHandler struct {
svc *app.AuthService
}
func NewAuthHandler(a *bootstrap.App) *AuthHandler {
return &AuthHandler{
svc: app.NewAuthService(
repository.NewAuthRepository(a.DB),
repository.NewTenantPlanRepository(a.DB),
a.KolProfiles,
a.JWT,
a.Sessions,
),
}
}
func (h *AuthHandler) Login(c *gin.Context) {
var req app.LoginRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
return
}
resp, err := h.svc.Login(c.Request.Context(), req)
if err != nil {
response.Error(c, err)
return
}
response.Success(c, resp)
}
func (h *AuthHandler) Refresh(c *gin.Context) {
var req app.RefreshRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
return
}
resp, err := h.svc.Refresh(c.Request.Context(), req)
if err != nil {
response.Error(c, err)
return
}
response.Success(c, resp)
}
func (h *AuthHandler) Me(c *gin.Context) {
actor, ok := auth.ActorFromCtx(c.Request.Context())
if !ok {
response.Error(c, response.ErrUnauthorized(40100, "not_authenticated", "not authenticated"))
return
}
resp, err := h.svc.Me(c.Request.Context(), actor)
if err != nil {
response.Error(c, err)
return
}
response.Success(c, resp)
}
func (h *AuthHandler) Logout(c *gin.Context) {
header := c.GetHeader("Authorization")
parts := strings.SplitN(header, " ", 2)
token := ""
if len(parts) == 2 {
token = parts[1]
}
_ = h.svc.Logout(c.Request.Context(), token)
response.Success(c, nil)
}