login and auth

This commit is contained in:
hptangxi
2025-07-02 20:44:30 +08:00
parent 8e1de8d29f
commit 7b372de6d8
12 changed files with 113 additions and 87 deletions

View File

@@ -1,6 +1,5 @@
import { authService } from "@/services/auth";
import { useAuthService } from "@/services/auth";
import axios from "axios";
import { useRouter } from "vue-router";
const http = axios.create({
baseURL: "/api",
@@ -10,7 +9,7 @@ const http = axios.create({
// 请求拦截器
http.interceptors.request.use(config => {
const authKey = authService.getAuthKey();
const authKey = localStorage.getItem("authKey");
if (authKey) {
config.headers.Authorization = `Bearer ${authKey}`;
}
@@ -22,8 +21,9 @@ http.interceptors.response.use(
response => response.data,
error => {
if (error.response && error.response.status === 401) {
authService.logout();
useRouter().push("/login");
const { logout } = useAuthService();
logout();
window.location.href = "/login";
}
console.error("API Error:", error);
return Promise.reject(error);