feat(auth+db): 实现注册登录核心逻辑,以及自动建表,用户信息落库
This commit is contained in:
@@ -4,15 +4,23 @@ import (
|
||||
"net/http"
|
||||
|
||||
"gen2d/internal/model"
|
||||
"gen2d/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var authSvc *service.AuthService
|
||||
|
||||
// InitAuthService 由 main 在启动时调用,注入 JWT 配置。
|
||||
func InitAuthService(jwtSecret string, jwtExpire int64) {
|
||||
authSvc = service.NewAuthService(jwtSecret, jwtExpire)
|
||||
}
|
||||
|
||||
// RegisterRequest 注册请求参数。
|
||||
type RegisterRequest struct {
|
||||
Username string `json:"username" binding:"required,min=3,max=32"` // 用户名
|
||||
Password string `json:"password" binding:"required,min=6,max=64"` // 密码
|
||||
Email string `json:"email" binding:"omitempty,email"` // 邮箱(可选)
|
||||
Username string `json:"username" binding:"required,min=3,max=32"`
|
||||
Password string `json:"password" binding:"required,min=6,max=64"`
|
||||
Email string `json:"email" binding:"omitempty,email"`
|
||||
}
|
||||
|
||||
// RegisterResponse 注册成功返回的用户信息。
|
||||
@@ -29,9 +37,14 @@ func Register(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: 调用 service 层创建用户
|
||||
user, err := authSvc.Register(c.Request.Context(), req.Username, req.Password, req.Email)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusConflict, model.Fail(http.StatusConflict, err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, model.OK(RegisterResponse{
|
||||
ID: 0,
|
||||
Username: req.Username,
|
||||
ID: user.ID,
|
||||
Username: user.Username,
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user