Files
cloudnest/internal/interface/http/handler/auth_handler.go
T

220 lines
6.2 KiB
Go

package handler
import (
"cloudnest/internal/application/auth"
"cloudnest/internal/application/captcha"
"cloudnest/internal/application/email"
"cloudnest/internal/pkg/errors"
"cloudnest/internal/pkg/response"
"github.com/gin-gonic/gin"
)
// AuthHandler handles HTTP requests for authentication operations.
// AuthHandler 处理认证操作的 HTTP 请求。
//
// This struct acts as the controller layer for authentication endpoints,
// converting HTTP requests to service calls and formatting responses.
//
// 此结构体作为认证端点的控制器层,将 HTTP 请求转换为服务调用并格式化响应。
type AuthHandler struct {
service *auth.AuthService
captchaService *captcha.CaptchaService
emailService *email.EmailService
}
// NewAuthHandler creates a new AuthHandler.
// NewAuthHandler 创建一个新的 AuthHandler。
//
// Parameters:
// - service: The authentication service to use.
// - captchaService: The captcha service to use.
// - emailService: The email service to use.
//
// Returns:
// - *AuthHandler: A pointer to the new AuthHandler instance.
//
// 参数:
// - service: 要使用的认证服务。
// - captchaService: 要使用的验证码服务。
// - emailService: 要使用的邮件服务。
//
// 返回值:
// - *AuthHandler: 新创建的 AuthHandler 实例指针。
func NewAuthHandler(service *auth.AuthService, captchaService *captcha.CaptchaService, emailService *email.EmailService) *AuthHandler {
return &AuthHandler{
service: service,
captchaService: captchaService,
emailService: emailService,
}
}
// Register handles user registration requests.
// Register 处理用户注册请求。
//
// Request:
// - Method: POST
// - Path: /api/v1/auth/register
// - Body: {"username": "string", "password": "string"}
//
// Response:
// - Success: 200 OK {"code": 0, "message": "注册成功"}
// - Conflict: 409 {"code": 409, "message": "用户名已存在"}
// - Bad Request: 400 {"code": 400, "message": "参数错误"}
//
// 请求:
// - 方法: POST
// - 路径: /api/v1/auth/register
// - 请求体: {"username": "string", "password": "string"}
//
// 响应:
// - 成功: 200 OK {"code": 0, "message": "注册成功"}
// - 冲突: 409 {"code": 409, "message": "用户名已存在"}
// - 请求错误: 400 {"code": 400, "message": "参数错误"}
func (h *AuthHandler) Register(c *gin.Context) {
var req auth.RegisterRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "参数错误")
return
}
if !h.captchaService.Verify(req.CaptchaID, req.CaptchaAnswer) {
response.BadRequest(c, "图形验证码错误")
return
}
if err := h.service.Register(req.Username, req.Password, req.Email, req.Nickname, req.EmailCode); err != nil {
if appErr, ok := errors.AsAppError(err); ok {
response.Error(c, appErr.Code, appErr.Message)
} else {
response.InternalError(c, err.Error())
}
return
}
response.SuccessMsg(c, "注册成功")
}
// Login handles user login requests.
// Login 处理用户登录请求。
//
// Request:
// - Method: POST
// - Path: /api/v1/auth/login
// - Body: {"username": "string", "password": "string"}
//
// Response:
// - Success: 200 OK {"code": 0, "message": "success", "data": {"token": "string"}}
// - Unauthorized: 401 {"code": 401, "message": "用户名或密码错误"}
// - Bad Request: 400 {"code": 400, "message": "参数错误"}
//
// 请求:
// - 方法: POST
// - 路径: /api/v1/auth/login
// - 请求体: {"username": "string", "password": "string"}
//
// 响应:
// - 成功: 200 OK {"code": 0, "message": "success", "data": {"token": "string"}}
// - 未授权: 401 {"code": 401, "message": "用户名或密码错误"}
// - 请求错误: 400 {"code": 400, "message": "参数错误"}
func (h *AuthHandler) Login(c *gin.Context) {
var req auth.LoginRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "参数错误")
return
}
if !h.captchaService.Verify(req.CaptchaID, req.CaptchaAnswer) {
response.BadRequest(c, "图形验证码错误")
return
}
token, _, err := h.service.Login(req.Username, req.Password)
if err != nil {
if appErr, ok := errors.AsAppError(err); ok {
response.Error(c, appErr.Code, appErr.Message)
} else {
response.InternalError(c, err.Error())
}
return
}
response.Success(c, auth.TokenResponse{Token: token})
}
func (h *AuthHandler) Profile(c *gin.Context) {
userCode := c.GetString("user_code")
if userCode == "" {
response.Unauthorized(c, "用户码不能为空")
return
}
user, err := h.service.GetUserInfo(userCode)
if err != nil {
if appErr, ok := errors.AsAppError(err); ok {
response.Error(c, appErr.Code, appErr.Message)
} else {
response.InternalError(c, err.Error())
}
return
}
response.Success(c, map[string]interface{}{
"username": user.Username,
"nickname": user.Nickname,
"email": user.Email,
"phone": user.Phone,
"storage_used": user.StorageUsed,
"storage_limit": user.StorageLimit,
})
}
func (h *AuthHandler) UpdateProfile(c *gin.Context) {
userCode := c.GetString("user_code")
if userCode == "" {
response.Unauthorized(c, "用户码不能为空")
return
}
var req auth.UpdateProfileRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "参数错误")
return
}
if err := h.service.UpdateProfile(userCode, req.Email, req.Phone, req.Nickname); err != nil {
if appErr, ok := errors.AsAppError(err); ok {
response.Error(c, appErr.Code, appErr.Message)
} else {
response.InternalError(c, err.Error())
}
return
}
response.SuccessMsg(c, "更新成功")
}
func (h *AuthHandler) ChangePassword(c *gin.Context) {
userCode := c.GetString("user_code")
if userCode == "" {
response.Unauthorized(c, "用户码不能为空")
return
}
var req auth.ChangePasswordRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "参数错误")
return
}
if err := h.service.ChangePassword(userCode, req.CurrentPassword, req.NewPassword); err != nil {
if appErr, ok := errors.AsAppError(err); ok {
response.Error(c, appErr.Code, appErr.Message)
} else {
response.InternalError(c, err.Error())
}
return
}
response.SuccessMsg(c, "密码修改成功")
}