feat: login
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import { authService } from "@/services/auth";
|
||||
import axios from "axios";
|
||||
import { useRouter } from "vue-router";
|
||||
|
||||
const http = axios.create({
|
||||
baseURL: "/api",
|
||||
@@ -8,9 +10,9 @@ const http = axios.create({
|
||||
|
||||
// 请求拦截器
|
||||
http.interceptors.request.use(config => {
|
||||
const token = localStorage.getItem("token");
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
const authKey = authService.getAuthKey();
|
||||
if (authKey) {
|
||||
config.headers.Authorization = `Bearer ${authKey}`;
|
||||
}
|
||||
return config;
|
||||
});
|
||||
@@ -19,6 +21,10 @@ http.interceptors.request.use(config => {
|
||||
http.interceptors.response.use(
|
||||
response => response.data,
|
||||
error => {
|
||||
if (error.response && error.response.status === 401) {
|
||||
authService.logout();
|
||||
useRouter().push("/login");
|
||||
}
|
||||
console.error("API Error:", error);
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
@@ -1,31 +0,0 @@
|
||||
import { createRouter, createWebHistory, type RouteRecordRaw } from "vue-router";
|
||||
|
||||
const routes: Array<RouteRecordRaw> = [
|
||||
{
|
||||
path: "/",
|
||||
name: "dashboard",
|
||||
component: () => import("@/views/Dashboard.vue"),
|
||||
},
|
||||
{
|
||||
path: "/keys",
|
||||
name: "keys",
|
||||
component: () => import("@/views/Keys.vue"),
|
||||
},
|
||||
{
|
||||
path: "/logs",
|
||||
name: "logs",
|
||||
component: () => import("@/views/Logs.vue"),
|
||||
},
|
||||
{
|
||||
path: "/settings",
|
||||
name: "settings",
|
||||
component: () => import("@/views/Settings.vue"),
|
||||
},
|
||||
];
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(import.meta.env.BASE_URL),
|
||||
routes,
|
||||
});
|
||||
|
||||
export default router;
|
Reference in New Issue
Block a user