|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"gofaster/internal/auth/controller"
|
|
|
|
"gofaster/internal/auth/repository"
|
|
|
|
"gofaster/internal/auth/service"
|
|
|
|
"gofaster/internal/shared/jwt"
|
|
|
|
"gofaster/internal/shared/middleware"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RegisterAuthRoutes 注册认证相关路由
|
|
|
|
func RegisterAuthRoutes(r *gin.RouterGroup, db *gorm.DB, jwtConfig middleware.JWTConfig) {
|
|
|
|
// 创建仓储层实例
|
|
|
|
userRepo := repository.NewUserRepository(db)
|
|
|
|
captchaRepo := repository.NewCaptchaRepository(db)
|
|
|
|
passwordPolicyRepo := repository.NewPasswordPolicyRepository(db)
|
|
|
|
passwordHistoryRepo := repository.NewPasswordHistoryRepository(db)
|
|
|
|
passwordResetRepo := repository.NewPasswordResetRepository(db)
|
|
|
|
|
|
|
|
// 创建JWT管理器
|
|
|
|
jwtManager := jwt.NewJWTManager(jwtConfig.SecretKey, jwtConfig.Issuer)
|
|
|
|
|
|
|
|
// 创建服务层实例
|
|
|
|
authService := service.NewAuthService(userRepo, captchaRepo, jwtManager)
|
|
|
|
userService := service.NewUserService(userRepo)
|
|
|
|
passwordService := service.NewPasswordService(userService, passwordPolicyRepo, passwordHistoryRepo, passwordResetRepo)
|
|
|
|
|
|
|
|
// 创建控制器实例
|
|
|
|
authController := controller.NewAuthController(authService)
|
|
|
|
passwordController := controller.NewPasswordController(passwordService, userService)
|
|
|
|
|
|
|
|
// 认证路由组
|
|
|
|
auth := r.Group("/auth")
|
|
|
|
{
|
|
|
|
// 公开接口(无需认证)
|
|
|
|
auth.POST("/login", authController.Login) // 用户登录
|
|
|
|
auth.GET("/captcha", authController.GenerateCaptcha) // 生成验证码
|
|
|
|
|
|
|
|
// 密码策略相关接口(无需认证)
|
|
|
|
auth.GET("/password-policy", passwordController.GetPasswordPolicy) // 获取密码策略
|
|
|
|
auth.POST("/validate-password", passwordController.ValidatePassword) // 验证密码强度
|
|
|
|
|
|
|
|
// 需要认证的接口
|
|
|
|
auth.Use(middleware.JWTAuth(jwtConfig))
|
|
|
|
{
|
|
|
|
auth.POST("/logout", authController.Logout) // 用户登出
|
|
|
|
auth.POST("/refresh", authController.RefreshToken) // 刷新令牌
|
|
|
|
auth.GET("/userinfo", authController.GetUserInfo) // 获取用户信息
|
|
|
|
|
|
|
|
// 密码管理接口
|
|
|
|
auth.POST("/change-password", passwordController.ChangePassword) // 修改密码
|
|
|
|
auth.GET("/password-status", passwordController.CheckPasswordStatus) // 检查密码状态
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|