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
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* 管理后台认证 API
|
* 管理后台认证 API
|
||||||
*
|
*
|
||||||
* 登录接口:/api/v1/admin/auth/login(管理端专用,后端检查管理员角色)
|
* 所有接口均使用管理端专属路由:/api/v1/admin/auth/*
|
||||||
* 其他接口:/api/v1/auth/*(与前台共用,通过 JWT 中的 client_type 区分)
|
* 后端对管理端路由做 JWT + admin 角色双重检查
|
||||||
*
|
*
|
||||||
* Token 在 Redis 中按 client_type 隔离存储:
|
* Token 在 Redis 中按 client_type 隔离存储:
|
||||||
* - 管理端:echo:auth:token:admin:{user_id}
|
* - 管理端:echo:auth:token:admin:{user_id}
|
||||||
@@ -23,16 +23,16 @@ export const login = (data) => {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 管理员登出
|
* 管理员登出
|
||||||
* POST /api/v1/auth/logout
|
* POST /api/v1/admin/auth/logout
|
||||||
*/
|
*/
|
||||||
export const logout = () => {
|
export const logout = () => {
|
||||||
return request.post('/api/v1/auth/logout')
|
return request.post('/api/v1/admin/auth/logout')
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取管理员个人信息
|
* 获取管理员个人信息
|
||||||
* GET /api/v1/auth/profile
|
* GET /api/v1/admin/auth/profile
|
||||||
*/
|
*/
|
||||||
export const getProfile = () => {
|
export const getProfile = () => {
|
||||||
return request.get('/api/v1/auth/profile')
|
return request.get('/api/v1/admin/auth/profile')
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
- Loading 状态防重复提交
|
- Loading 状态防重复提交
|
||||||
- 登录成功跳转仪表盘
|
- 登录成功跳转仪表盘
|
||||||
|
|
||||||
对应 API:POST /api/v1/auth/login
|
对应 API:POST /api/v1/admin/auth/login
|
||||||
-->
|
-->
|
||||||
<template>
|
<template>
|
||||||
<div class="login-page">
|
<div class="login-page">
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package auth
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/echochat/backend/app/auth/controller"
|
"github.com/echochat/backend/app/auth/controller"
|
||||||
|
"github.com/echochat/backend/app/constants"
|
||||||
|
"github.com/echochat/backend/pkg/middleware"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,6 +16,8 @@ func RegisterRoutes(
|
|||||||
adminCtrl *controller.AdminAuthController,
|
adminCtrl *controller.AdminAuthController,
|
||||||
authMiddleware gin.HandlerFunc,
|
authMiddleware gin.HandlerFunc,
|
||||||
) {
|
) {
|
||||||
|
// ======================== 前台用户端 ========================
|
||||||
|
|
||||||
// 前台公开路由(无需认证)
|
// 前台公开路由(无需认证)
|
||||||
public := r.Group("/api/v1/auth")
|
public := r.Group("/api/v1/auth")
|
||||||
{
|
{
|
||||||
@@ -32,9 +36,22 @@ func RegisterRoutes(
|
|||||||
authed.PUT("/password", ctrl.ChangePassword)
|
authed.PUT("/password", ctrl.ChangePassword)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 后台管理认证路由(无需认证,登录后在 Service 层检查管理员角色)
|
// ======================== 后台管理端 ========================
|
||||||
admin := r.Group("/api/v1/admin/auth")
|
|
||||||
|
// 管理端公开路由(无需认证,登录后在 Service 层检查管理员角色)
|
||||||
|
adminPublic := r.Group("/api/v1/admin/auth")
|
||||||
{
|
{
|
||||||
admin.POST("/login", adminCtrl.AdminLogin)
|
adminPublic.POST("/login", adminCtrl.AdminLogin)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 管理端需认证路由(JWT + admin 角色双重检查)
|
||||||
|
// 复用 AuthController handler,JWT 中的 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user