feat(auth): 重构认证模块,添加OTP发送和登录功能

This commit is contained in:
2025-12-30 18:10:24 +08:00
parent 3952d80d30
commit 6d7f4ad1c6
8 changed files with 149 additions and 24 deletions

View File

@@ -0,0 +1,41 @@
package auth
import (
"quyun/v2/app/http/v1/dto"
"quyun/v2/app/services"
"github.com/gofiber/fiber/v3"
)
// @provider
type Auth struct{}
// SendOTP sends an OTP to the provided phone number.
//
// @Router /v1/auth/otp [post]
// @Summary Send OTP
// @Description Send OTP to phone number
// @Tags Auth
// @Accept json
// @Produce json
// @Param form body dto.SendOTPForm true "Phone number"
// @Success 200 {object} string "OTP sent"
// @Bind form body
func (a *Auth) SendOTP(ctx fiber.Ctx, form *dto.SendOTPForm) error {
return services.User.SendOTP(ctx.Context(), form.Phone)
}
// Login logs in or registers a user with OTP.
//
// @Router /v1/auth/login [post]
// @Summary Login or Register with OTP
// @Description Login or register user using phone number and OTP
// @Tags Auth
// @Accept json
// @Produce json
// @Param form body dto.LoginForm true "Login form"
// @Success 200 {object} dto.LoginResponse
// @Bind form body
func (a *Auth) Login(ctx fiber.Ctx, form *dto.LoginForm) (*dto.LoginResponse, error) {
return services.User.LoginWithOTP(ctx.Context(), form.Phone, form.OTP)
}

View File

@@ -0,0 +1,36 @@
package dto
import "quyun/v2/pkg/consts"
type SendOTPForm struct {
Phone string `json:"phone"`
}
type LoginForm struct {
Phone string `json:"phone"`
OTP string `json:"otp"`
}
type LoginResponse struct {
Token string `json:"token"`
User *User `json:"user"`
}
type User struct {
ID string `json:"id"`
Phone string `json:"phone"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
Gender consts.Gender `json:"gender"`
Bio string `json:"bio"`
Birthday string `json:"birthday"` // YYYY-MM-DD
Location *Location `json:"location"`
Balance float64 `json:"balance"`
Points int64 `json:"points"`
IsRealNameVerified bool `json:"is_real_name_verified"`
}
type Location struct {
Province string `json:"province"`
City string `json:"city"`
}

View File

@@ -0,0 +1,37 @@
package auth
import (
"quyun/v2/app/middlewares"
"go.ipao.vip/atom"
"go.ipao.vip/atom/container"
"go.ipao.vip/atom/contracts"
"go.ipao.vip/atom/opt"
)
func Provide(opts ...opt.Option) error {
if err := container.Container.Provide(func() (*Auth, error) {
obj := &Auth{}
return obj, nil
}); err != nil {
return err
}
if err := container.Container.Provide(func(
auth *Auth,
middlewares *middlewares.Middlewares,
) (contracts.HttpRoute, error) {
obj := &Routes{
auth: auth,
middlewares: middlewares,
}
if err := obj.Prepare(); err != nil {
return nil, err
}
return obj, nil
}, atom.GroupRoutes); err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,57 @@
// Code generated by atomctl. DO NOT EDIT.
// Package auth provides HTTP route definitions and registration
// for the quyun/v2 application.
package auth
import (
"quyun/v2/app/http/v1/dto"
"quyun/v2/app/middlewares"
"github.com/gofiber/fiber/v3"
log "github.com/sirupsen/logrus"
_ "go.ipao.vip/atom"
_ "go.ipao.vip/atom/contracts"
. "go.ipao.vip/atom/fen"
)
// Routes implements the HttpRoute contract and provides route registration
// for all controllers in the auth module.
//
// @provider contracts.HttpRoute atom.GroupRoutes
type Routes struct {
log *log.Entry `inject:"false"`
middlewares *middlewares.Middlewares
// Controller instances
auth *Auth
}
// Prepare initializes the routes provider with logging configuration.
func (r *Routes) Prepare() error {
r.log = log.WithField("module", "routes.auth")
r.log.Info("Initializing routes module")
return nil
}
// Name returns the unique identifier for this routes provider.
func (r *Routes) Name() string {
return "auth"
}
// Register registers all HTTP routes with the provided fiber router.
// Each route is registered with its corresponding controller action and parameter bindings.
func (r *Routes) Register(router fiber.Router) {
// Register routes for controller: Auth
r.log.Debugf("Registering route: Post /v1/auth/login -> auth.Login")
router.Post("/v1/auth/login"[len(r.Path()):], DataFunc1(
r.auth.Login,
Body[dto.LoginForm]("form"),
))
r.log.Debugf("Registering route: Post /v1/auth/otp -> auth.SendOTP")
router.Post("/v1/auth/otp"[len(r.Path()):], Func1(
r.auth.SendOTP,
Body[dto.SendOTPForm]("form"),
))
r.log.Info("Successfully registered all routes")
}

View File

@@ -0,0 +1,9 @@
package auth
func (r *Routes) Path() string {
return "/auth"
}
func (r *Routes) Middlewares() []any {
return []any{}
}