Files
backend_v2/internal/handler/auth_handler.go
toom1996 9c3403a903 update
2026-08-30 10:45:34 +08:00

41 lines
844 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package handler
import (
"net/http"
"fashionapi/internal/middleware"
"fashionapi/internal/pkg/response"
"fashionapi/internal/service"
"github.com/gin-gonic/gin"
)
// AuthHandler 账号体系接口(JWT 无状态)。
type AuthHandler struct {
auth service.AuthService
}
// NewAuthHandler 创建账号 handler。
func NewAuthHandler(auth service.AuthService) *AuthHandler {
return &AuthHandler{auth: auth}
}
// Me 当前登录用户,需 Authorization: Bearer <token>。
//
// GET /api/auth/me
// 成功:200 { user }
func (h *AuthHandler) Me(c *gin.Context) {
userID, ok := middleware.UserIDFrom(c)
if !ok {
response.Error(c, http.StatusUnauthorized, "unauthorized")
return
}
user, err := h.auth.Me(c.Request.Context(), userID)
if err != nil {
fail(c, err)
return
}
c.JSON(http.StatusOK, gin.H{"user": user})
}