Files
digital-psychology/apps/api/internal/handler/auth.go
T
jackyu66gitandCursor 5ceb3ce749
ci / h5 (push) Canceled after 0s
ci / api (push) Canceled after 0s
ci / ess-docs (push) Canceled after 0s
feat(ECR-010): Ops-E 系统运营;修复登出解绑;P2 Complete
落地管理员 RBAC/封禁/推送任务 stub,logout 解绑 device 并统一各页 ensureAccount,同时收口 P2 生日生成与状态文档。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-11 18:54:59 +08:00

107 lines
2.9 KiB
Go

package handler
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/yuxingu/digital-psychology/apps/api/internal/middleware"
"github.com/yuxingu/digital-psychology/apps/api/internal/service/auth"
"github.com/yuxingu/digital-psychology/apps/api/pkg/response"
)
// AuthHandler serves /api/v1/auth/*.
type AuthHandler struct {
Svc *auth.Service
}
// Register mounts auth routes. Public register/login; me/logout need device (+ session).
func (h *AuthHandler) Register(api *gin.RouterGroup) {
g := api.Group("/auth")
g.POST("/register", h.RegisterAccount)
g.POST("/login", h.Login)
g.POST("/logout", h.Logout)
g.GET("/me", h.Me)
}
type authBody struct {
Phone string `json:"phone"`
Password string `json:"password"`
Nickname string `json:"nickname"`
}
// RegisterAccount handles POST /auth/register (DeviceAuth required on group).
func (h *AuthHandler) RegisterAccount(c *gin.Context) {
userID, ok := middleware.UserIDFromContext(c)
if !ok {
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
return
}
var body authBody
if err := c.ShouldBindJSON(&body); err != nil {
response.Fail(c, http.StatusBadRequest, 10000, "invalid request")
return
}
deviceKey := c.GetHeader(middleware.DeviceKeyHeader)
res, err := h.Svc.Register(c.Request.Context(), userID, deviceKey, body.Phone, body.Password, body.Nickname)
if err != nil {
response.Fail(c, http.StatusBadRequest, 40110, err.Error())
return
}
response.OK(c, res)
}
// Login handles POST /auth/login (open mode: any phone+password).
func (h *AuthHandler) Login(c *gin.Context) {
userID, ok := middleware.UserIDFromContext(c)
if !ok {
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
return
}
var body authBody
if err := c.ShouldBindJSON(&body); err != nil {
response.Fail(c, http.StatusBadRequest, 10000, "invalid request")
return
}
deviceKey := c.GetHeader(middleware.DeviceKeyHeader)
res, err := h.Svc.Login(c.Request.Context(), userID, deviceKey, body.Phone, body.Password)
if err != nil {
response.Fail(c, http.StatusBadRequest, 40111, err.Error())
return
}
c.Set(string(middleware.UserIDKey), res.User.ID)
response.OK(c, res)
}
// Logout handles POST /auth/logout.
func (h *AuthHandler) Logout(c *gin.Context) {
tok := bearerToken(c)
deviceKey := c.GetHeader(middleware.DeviceKeyHeader)
_ = h.Svc.Logout(c.Request.Context(), tok, deviceKey)
response.OK(c, gin.H{"ok": true})
}
// Me handles GET /auth/me.
func (h *AuthHandler) Me(c *gin.Context) {
userID, ok := middleware.UserIDFromContext(c)
if !ok {
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
return
}
me, err := h.Svc.Me(c.Request.Context(), userID)
if err != nil {
response.Fail(c, http.StatusUnauthorized, 40112, "请先登录")
return
}
response.OK(c, me)
}
func bearerToken(c *gin.Context) string {
h := c.GetHeader("Authorization")
if strings.HasPrefix(strings.ToLower(h), "bearer ") {
return strings.TrimSpace(h[7:])
}
return ""
}