后台
This commit is contained in:
7
src/utils/authSessionState.js
Normal file
7
src/utils/authSessionState.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import { ref } from 'vue';
|
||||
|
||||
// This flag is used by the router guard to know if a navigation
|
||||
// is occurring immediately after a successful login.
|
||||
// If true, the guard can choose to bypass an immediate session check (e.g., getUserInfo)
|
||||
// for that first navigation, assuming the new token is valid.
|
||||
export const justLoggedIn = ref(false);
|
||||
70
src/utils/jwt.js
Normal file
70
src/utils/jwt.js
Normal file
@@ -0,0 +1,70 @@
|
||||
import axiosInstance from '../api/axiosConfig';
|
||||
import router from '../router'; // 引入 Vue Router 实例
|
||||
import { justLoggedIn } from './authSessionState'; // Import the flag
|
||||
|
||||
const USER_INFO_URL = '/user'; // 获取用户信息的API端点
|
||||
|
||||
/**
|
||||
* 检查Token是否存在且(理论上)是否在有效期内。
|
||||
* 服务端 /user接口会验证实际有效期。
|
||||
* @returns {boolean} 如果存在token则返回true,否则false。
|
||||
*/
|
||||
export const hasValidToken = () => {
|
||||
const token = localStorage.getItem('access_token');
|
||||
// 简单的存在性检查,实际有效期由 /user 接口判断
|
||||
return !!token;
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取当前用户信息。
|
||||
* 如果Token无效或过期,API会返回401,响应拦截器将处理清除token。
|
||||
* @returns {Promise<Object|null>} 用户信息对象或null(如果未登录或获取失败)。
|
||||
*/
|
||||
export const getUserInfo = async () => {
|
||||
if (!hasValidToken()) {
|
||||
// console.log('jwt.js: No token found, skipping getUserInfo.');
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
// console.log('jwt.js: Attempting to fetch user info from', USER_INFO_URL);
|
||||
const response = await axiosInstance.get(USER_INFO_URL);
|
||||
// console.log('jwt.js: User info received:', response.data);
|
||||
return response.data; // 假设API成功时返回用户信息对象
|
||||
} catch (error) {
|
||||
// console.error('jwt.js: Error fetching user info:', error.response ? error.response.status : error.message);
|
||||
// 401错误会被响应拦截器处理(清除token),然后错误会传播到这里
|
||||
// 其他网络错误等也会被捕获
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 处理用户登出。
|
||||
* 仅清除本地存储的认证信息。
|
||||
*/
|
||||
export const logoutUser = () => { // 不再是 async,因为它不执行异步导航
|
||||
// console.log('jwt.js: logoutUser called. Clearing local storage.');
|
||||
localStorage.removeItem('access_token');
|
||||
localStorage.removeItem('user_id'); // 如果您也存储了user_id
|
||||
// 导航将由调用者(如路由守卫)处理
|
||||
};
|
||||
|
||||
/**
|
||||
* 登录成功后调用,存储token和用户信息,并处理重定向。
|
||||
* @param {string} accessToken - 从登录API获取的访问令牌。
|
||||
* @param {string} userId - 用户ID。
|
||||
*/
|
||||
export const loginSuccess = (accessToken, userId) => {
|
||||
// console.log('jwt.js: loginSuccess. Storing token and user ID.');
|
||||
localStorage.setItem('access_token', accessToken);
|
||||
if (userId) {
|
||||
localStorage.setItem('user_id', userId);
|
||||
}
|
||||
|
||||
justLoggedIn.value = true; // Set the flag before navigation
|
||||
|
||||
// 登录成功后重定向
|
||||
// 尝试获取之前尝试访问的路径,否则重定向到默认路径(例如仪表盘)
|
||||
const redirectPath = router.currentRoute.value.query.redirect || '/backend/dashboard';
|
||||
router.replace(redirectPath);
|
||||
};
|
||||
Reference in New Issue
Block a user