feat: update

This commit is contained in:
Czw 2023-10-08 19:12:35 +08:00
parent 9229766db4
commit 18525a99b7
2 changed files with 8 additions and 19 deletions

View file

@ -2,15 +2,11 @@ from rest_framework_simplejwt.authentication import JWTAuthentication
from rest_framework_simplejwt.exceptions import InvalidToken
from extensions.exceptions import NotAuthenticated
from apps.system.models import User
from django.conf import settings
class BaseAuthentication(JWTAuthentication):
def authenticate(self, request):
# if settings.DEBUG:
# return User.objects.all().first(), {}
if (header := self.get_header(request)) is None:
return None
@ -20,12 +16,8 @@ class BaseAuthentication(JWTAuthentication):
try:
validated_token = self.get_validated_token(raw_token)
user = User.objects.get(id=validated_token['user_id'])
except KeyError:
raise NotAuthenticated('令牌不包含用户标识')
except User.DoesNotExist:
raise NotAuthenticated('用户不存在')
except InvalidToken:
raise NotAuthenticated('令牌无效')
except (KeyError, InvalidToken, User.DoesNotExist):
return None
return user, validated_token

View file

@ -1,7 +1,6 @@
import { message } from "ant-design-vue";
import Cookies from "js-cookie";
import router from "@/router";
import { T } from "@/locales";
import axios from "axios";
const GET_TOKEN_URL = "/user/get_token/";
@ -12,16 +11,16 @@ let requestQueue = [],
const instance = axios.create({
baseURL: "/api/",
timeout: 6000000,
timeout: 10000,
headers: { "Content-Type": "application/json" },
});
instance.interceptors.request.use(
(config) => {
if (!config.url.includes(GET_TOKEN_URL) && !config.url.includes(REFRESH_TOKEN_URL)) {
config.headers["Authorization"] = "Bearer " + Cookies.get("access");
config.headers.Authorization = "Bearer " + Cookies.get("access");
}
config.headers["Accept-Language"] = Cookies.get("language") || "zh-Hans";
console.info("Send request:", config);
return config;
},
@ -39,7 +38,7 @@ instance.interceptors.response.use(
console.error("Request error:", error.response);
if (error.response.status >= 500) {
message.error(T("服务器错误"));
message.error("服务器错误");
return Promise.reject(error);
}
@ -63,7 +62,7 @@ instance.interceptors.response.use(
.catch((error) => {
if (error.response.status == 401) {
redirectLogin();
message.error(T("令牌过期, 请重新登录"));
message.error("令牌过期, 请重新登录");
}
return Promise.reject(error);
})
@ -73,9 +72,7 @@ instance.interceptors.response.use(
}
}
if (error.response.data.detail) {
message.error(error.response.data.detail);
}
message.error(error.response.data.detail);
return Promise.reject(error);
}
);