You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.3 KiB
72 lines
2.3 KiB
package routes |
|
|
|
import ( |
|
"log" |
|
|
|
"gofaster/internal/auth/controller" |
|
"gofaster/internal/auth/repository" |
|
"gofaster/internal/auth/service" |
|
"gofaster/internal/shared/middleware" |
|
|
|
"github.com/gin-gonic/gin" |
|
"gorm.io/gorm" |
|
) |
|
|
|
// RegisterAuthRoutes 注册认证相关路由 |
|
func RegisterAuthRoutes(router *gin.RouterGroup, db *gorm.DB) { |
|
// 初始化仓库 |
|
userRepo := repository.NewUserRepository(db) |
|
passwordPolicyRepo := repository.NewPasswordPolicyRepository(db) |
|
passwordHistoryRepo := repository.NewPasswordHistoryRepository(db) |
|
passwordResetRepo := repository.NewPasswordResetRepository(db) |
|
|
|
// 初始化服务 |
|
userService := service.NewUserService(userRepo) |
|
authService := service.NewAuthService(userRepo) |
|
passwordService := service.NewPasswordService( |
|
userRepo, |
|
passwordPolicyRepo, |
|
passwordHistoryRepo, |
|
passwordResetRepo, |
|
) |
|
|
|
// 初始化控制器 |
|
userController := controller.NewUserController(userService) |
|
authController := controller.NewAuthController(authService) |
|
passwordController := controller.NewPasswordController(passwordService, userService) |
|
|
|
// 公开路由(无需认证) |
|
public := router.Group("/auth") |
|
{ |
|
public.POST("/login", authController.Login) |
|
public.POST("/register", userController.Register) |
|
public.GET("/password-policy", passwordController.GetPasswordPolicy) |
|
public.POST("/validate-password", passwordController.ValidatePassword) |
|
} |
|
|
|
// 需要认证的路由 |
|
auth := router.Group("/auth") |
|
auth.Use(middleware.JWTAuth()) |
|
{ |
|
auth.POST("/logout", authController.Logout) |
|
auth.GET("/profile", userController.GetProfile) |
|
auth.PUT("/profile", userController.UpdateProfile) |
|
auth.POST("/change-password", passwordController.ChangePassword) |
|
auth.GET("/password-status", passwordController.CheckPasswordStatus) |
|
} |
|
|
|
// 管理员路由 |
|
admin := router.Group("/auth/admin") |
|
admin.Use(middleware.JWTAuth(), middleware.Permission("auth", "admin")) |
|
{ |
|
admin.GET("/users", userController.GetUsers) |
|
admin.POST("/users", userController.CreateUser) |
|
admin.GET("/users/:id", userController.GetUser) |
|
admin.PUT("/users/:id", userController.UpdateUser) |
|
admin.DELETE("/users/:id", userController.DeleteUser) |
|
admin.POST("/users/:id/reset-password", passwordController.ResetPassword) |
|
admin.PUT("/password-policy", passwordController.UpdatePasswordPolicy) |
|
} |
|
|
|
log.Printf("✅ 认证路由注册完成") |
|
}
|
|
|