Files
EchoChat/backend/go-service/app/auth/router.go
bujinyuan 13c6b44352 fix: 管理端所有认证接口统一使用 /api/v1/admin/auth/* 路由
后端:
- 新增管理端认证路由组 /api/v1/admin/auth/(JWT + admin 角色双重检查)
- 管理端登出:POST /api/v1/admin/auth/logout
- 管理端获取个人信息:GET /api/v1/admin/auth/profile
- 管理端更新个人信息:PUT /api/v1/admin/auth/profile
- 管理端修改密码:PUT /api/v1/admin/auth/password

前端:
- admin/src/api/auth.js 所有接口全部改为 /api/v1/admin/auth/*
- 登出和获取个人信息不再使用前台的 /api/v1/auth/ 路由

Made-with: Cursor
2026-03-02 14:33:18 +08:00

58 lines
1.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package auth
import (
"github.com/echochat/backend/app/auth/controller"
"github.com/echochat/backend/app/constants"
"github.com/echochat/backend/pkg/middleware"
"github.com/gin-gonic/gin"
)
// RegisterRoutes 注册 auth 模块的所有路由
// 包含前台公开路由、前台需认证路由、后台管理认证路由
// authMiddleware 为 JWT 认证中间件,由外部传入以解耦
func RegisterRoutes(
r *gin.Engine,
ctrl *controller.AuthController,
adminCtrl *controller.AdminAuthController,
authMiddleware gin.HandlerFunc,
) {
// ======================== 前台用户端 ========================
// 前台公开路由(无需认证)
public := r.Group("/api/v1/auth")
{
public.POST("/register", ctrl.Register)
public.POST("/login", ctrl.Login)
public.POST("/refresh-token", ctrl.RefreshToken)
}
// 前台需认证路由
authed := r.Group("/api/v1/auth")
authed.Use(authMiddleware)
{
authed.POST("/logout", ctrl.Logout)
authed.GET("/profile", ctrl.GetProfile)
authed.PUT("/profile", ctrl.UpdateProfile)
authed.PUT("/password", ctrl.ChangePassword)
}
// ======================== 后台管理端 ========================
// 管理端公开路由(无需认证,登录后在 Service 层检查管理员角色)
adminPublic := r.Group("/api/v1/admin/auth")
{
adminPublic.POST("/login", adminCtrl.AdminLogin)
}
// 管理端需认证路由JWT + admin 角色双重检查)
// 复用 AuthController handlerJWT 中的 client_type=admin 保证 Redis 正确隔离
adminAuthed := r.Group("/api/v1/admin/auth")
adminAuthed.Use(authMiddleware, middleware.RequireRole(constants.RoleAdmin, constants.RoleSuperAdmin))
{
adminAuthed.POST("/logout", ctrl.Logout)
adminAuthed.GET("/profile", ctrl.GetProfile)
adminAuthed.PUT("/profile", ctrl.UpdateProfile)
adminAuthed.PUT("/password", ctrl.ChangePassword)
}
}