You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
3.7 KiB
TypeScript
115 lines
3.7 KiB
TypeScript
|
1 month ago
|
// middleware/auth.ts
|
||
|
|
import { useAuthStore } from '~/stores/user';
|
||
|
|
// import redis from '~/utils/redis';
|
||
|
|
// import { getCookie } from '@/utils/cookie'
|
||
|
|
|
||
|
|
export default defineNuxtRouteMiddleware((to, from) => {
|
||
|
|
|
||
|
|
console.error("环境:需要权限", to.meta.requiresAuth, "isServer:", import.meta.server, "isClient:", import.meta.client, "路径:", to.fullPath);
|
||
|
|
let token = '';
|
||
|
|
let isAuthed = false;
|
||
|
|
let requiresAuth = to.meta.requiresAuth;
|
||
|
|
// const authStore = useAuthStore();
|
||
|
|
// 初始化认证状态
|
||
|
|
// authStore.initializeAuth();
|
||
|
|
// const isAuthed = authStore.isAuthenticated;
|
||
|
|
if (import.meta.client) {
|
||
|
|
const authStore = useAuthStore();
|
||
|
|
authStore.initializeAuth();
|
||
|
|
token = authStore.token;
|
||
|
|
isAuthed = authStore.isAuthenticated;
|
||
|
|
console.log("client 获取 token", token, isAuthed);
|
||
|
|
};
|
||
|
|
// // 白名单:无需验证的路由(如登录页、注册页)
|
||
|
|
// const whiteList = ['/login', '/register'];
|
||
|
|
// if (whiteList.includes(to.path)) return; // 放行白名单
|
||
|
|
// 检查路由是否需要验证
|
||
|
|
// const requiresAuth = to.meta.requiresAuth;
|
||
|
|
|
||
|
|
// TODO 通过后台验证token的有效性
|
||
|
|
|
||
|
|
if (import.meta.server) {
|
||
|
|
const headers = useRequestHeaders(['cookie']);
|
||
|
|
const tokenPair = headers.cookie?.split(';').find(item => item.trim().startsWith('token='));
|
||
|
|
token = tokenPair ? tokenPair.split('=')[1] : '';
|
||
|
|
if (token) {
|
||
|
|
isAuthed = true;
|
||
|
|
}
|
||
|
|
// const response = useResponse();
|
||
|
|
console.log("server 授权信息:", tokenPair);
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
// await new Promise<void>(resolve => {
|
||
|
|
// const check = () => {
|
||
|
|
// if (authStore.isInitialized) {
|
||
|
|
// resolve();
|
||
|
|
// } else {
|
||
|
|
// authStore.initializeAuth();
|
||
|
|
// setTimeout(check, 50);
|
||
|
|
// }
|
||
|
|
// };
|
||
|
|
// check();
|
||
|
|
// });
|
||
|
|
|
||
|
|
// console.log("import.meta.server", headers.cookie);
|
||
|
|
|
||
|
|
// // 解析 cookie 并获取 token
|
||
|
|
// const cookies = headers.cookie.split(';').reduce((acc, cookie) => {
|
||
|
|
// const [key, value] = cookie.trim().split('=');
|
||
|
|
// acc[key] = value;
|
||
|
|
// return acc;
|
||
|
|
// }, {} as Record<string, string>);
|
||
|
|
|
||
|
|
// if (cookies.token) {
|
||
|
|
// authStore.isAuthenticated = true;
|
||
|
|
// }
|
||
|
|
// if (requiresAuth && authStore.isAuthenticated) {
|
||
|
|
// return navigateTo(`/login?redirect=${encodeURIComponent(to.fullPath)}`);
|
||
|
|
// }
|
||
|
|
// const req = useRequestHeaders(['cookie']);
|
||
|
|
// let tokenStr = '';
|
||
|
|
// if (req.cookie) {
|
||
|
|
// tokenStr = req.cookie.split(';').find(c => c.trim().startsWith('token='));
|
||
|
|
// }
|
||
|
|
|
||
|
|
// let token = !!tokenStr?.split('=')[1];
|
||
|
|
// console.log("验证权限获取到的token:", req, token);
|
||
|
|
|
||
|
|
// console.log("授权验证:", requiresAuth, authStore.isAuthenticated, authStore.isInitialized);
|
||
|
|
if (requiresAuth && !isAuthed) {
|
||
|
|
console.log("跳转路径", to.fullPath);
|
||
|
|
|
||
|
|
return navigateTo(`/login?redirect=${encodeURIComponent(to.fullPath)}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
// // 如果用户已登录但访问的是登录页面
|
||
|
|
if (to.path === '/login' && isAuthed) {
|
||
|
|
console.log("跳转至首页");
|
||
|
|
return navigateTo('/');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// if (!authStore.isInitialized) {
|
||
|
|
// await new Promise(resolve => setTimeout(resolve, 50));
|
||
|
|
// };
|
||
|
|
// if (import.meta.client) {
|
||
|
|
|
||
|
|
// authStore.initializeAuth();
|
||
|
|
// console.log("访问的路径:", to.path, authStore.isAuthenticated);
|
||
|
|
// // 如果访问的是需要认证的页面且用户未登录
|
||
|
|
// if (requiresAuth && !authStore.isAuthenticated) {
|
||
|
|
// // if (requiresAuth) {
|
||
|
|
// console.log("访问的路径:", to.path);
|
||
|
|
// return navigateTo(`/login?redirect=${encodeURIComponent(to.fullPath)}`);
|
||
|
|
// }
|
||
|
|
|
||
|
|
// // 如果用户已登录但访问的是登录页面
|
||
|
|
// if (to.path === '/login') {
|
||
|
|
// return navigateTo('/');
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
);
|