@@ -257,7 +257,7 @@ export const authController = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
// 用户登出
|
// 用户登出
|
||||||
async logout(req: Request, res: Response) {
|
async logout(req: AuthRequest, res: Response) {
|
||||||
const token = req.headers.authorization?.substring(7);
|
const token = req.headers.authorization?.substring(7);
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
@@ -267,26 +267,18 @@ export const authController = {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create audit log (if we have user info from token)
|
// Create audit log
|
||||||
try {
|
if (req.user) {
|
||||||
if (token) {
|
await prisma.auditLog.create({
|
||||||
const decoded = jwt.verify(token, "pandora") as any;
|
data: {
|
||||||
if (decoded && decoded.userId) {
|
userId: req.user.id,
|
||||||
await prisma.auditLog.create({
|
action: 'USER_LOGOUT',
|
||||||
data: {
|
resource: 'user',
|
||||||
userId: decoded.userId,
|
resourceId: req.user.id,
|
||||||
action: 'USER_LOGOUT',
|
ipAddress: (req.headers['x-forwarded-for'] as string) || req.socket.remoteAddress || null,
|
||||||
resource: 'user',
|
userAgent: req.get('User-Agent') ?? null,
|
||||||
resourceId: decoded.userId,
|
|
||||||
ipAddress: (req.headers['x-forwarded-for'] as string) || req.socket.remoteAddress || null,
|
|
||||||
userAgent: req.get('User-Agent') ?? null,
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
} catch (error) {
|
|
||||||
// Token无效,不记录审计日志
|
|
||||||
console.log('登出时token无效,跳过审计日志记录');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res.json({ message: '登出成功' });
|
res.json({ message: '登出成功' });
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ router.post('/login', [
|
|||||||
], authController.login);
|
], authController.login);
|
||||||
|
|
||||||
// Logout
|
// Logout
|
||||||
router.post('/logout', authController.logout);
|
router.post('/logout', authMiddleware, authController.logout);
|
||||||
|
|
||||||
// Get current user
|
// Get current user
|
||||||
router.get('/me', authMiddleware, authController.getCurrentUser);
|
router.get('/me', authMiddleware, authController.getCurrentUser);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { createRouter, createWebHistory, type RouteRecordRaw, type NavigationGuardNext, type RouteLocationNormalized } from 'vue-router'
|
import type { RouteRecordRaw, NavigationGuardNext, RouteLocationNormalized } from 'vue-router'
|
||||||
|
import { createRouter, createWebHistory } from 'vue-router'
|
||||||
import { useAuthStore } from '@/stores/auth'
|
import { useAuthStore } from '@/stores/auth'
|
||||||
import { adminAuth } from '@/utils/auth'
|
import { adminAuth } from '@/utils/auth'
|
||||||
|
|
||||||
@@ -75,6 +76,11 @@ router.beforeEach(async (
|
|||||||
document.title = `${title} - AI`
|
document.title = `${title} - AI`
|
||||||
|
|
||||||
const authStore = useAuthStore()
|
const authStore = useAuthStore()
|
||||||
|
// 用户已登录,重定向到dashboard
|
||||||
|
if (to.path === '/' && authStore.isLoggedIn) {
|
||||||
|
next({ name: 'Dashboard' })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// 检查是否需要用户认证
|
// 检查是否需要用户认证
|
||||||
if (to.meta.requiresAuth) {
|
if (to.meta.requiresAuth) {
|
||||||
@@ -92,12 +98,6 @@ router.beforeEach(async (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 用户已登录且访问首页,重定向到dashboard
|
|
||||||
if (to.path === '/' && authStore.isLoggedIn) {
|
|
||||||
next({ name: 'Dashboard' })
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
next()
|
next()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -41,11 +41,6 @@ api.interceptors.response.use(
|
|||||||
},
|
},
|
||||||
(error) => {
|
(error) => {
|
||||||
if (error.response?.status === 401) {
|
if (error.response?.status === 401) {
|
||||||
// 如果是登出请求,不要自动重定向
|
|
||||||
if (error.config?.url?.includes('/auth/logout')) {
|
|
||||||
return Promise.reject(error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Token过期或无效,清除所有认证状态
|
// Token过期或无效,清除所有认证状态
|
||||||
userAuth.logout()
|
userAuth.logout()
|
||||||
adminAuth.logout()
|
adminAuth.logout()
|
||||||
|
|||||||
@@ -202,7 +202,7 @@ const loadUserAccounts = async () => {
|
|||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
try {
|
try {
|
||||||
// 初始化认证状态
|
// 初始化认证状态
|
||||||
await authStore.initAuth()
|
authStore.initAuth()
|
||||||
|
|
||||||
// 确保用户已登录
|
// 确保用户已登录
|
||||||
if (!authStore.isLoggedIn) {
|
if (!authStore.isLoggedIn) {
|
||||||
@@ -222,8 +222,6 @@ onMounted(async () => {
|
|||||||
router.push('/')
|
router.push('/')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 其他错误,继续尝试加载账号
|
|
||||||
console.warn('获取用户信息失败,但继续加载账号:', error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载用户账号
|
// 加载用户账号
|
||||||
|
|||||||
Reference in New Issue
Block a user