login and auth
This commit is contained in:
@@ -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);
|
||||
|
Reference in New Issue
Block a user