mirror of
https://github.com/whyour/qinglong.git
synced 2026-07-16 17:09:20 +08:00
优化登录验证、退出逻辑
This commit is contained in:
+27
@@ -0,0 +1,27 @@
|
||||
import { history } from 'umi';
|
||||
import { request } from '@/utils/http';
|
||||
import config from '@/utils/config';
|
||||
|
||||
export function render(oldRender: any) {
|
||||
if (history.location.pathname === '/login') {
|
||||
oldRender();
|
||||
}
|
||||
request
|
||||
.get(`${config.apiPrefix}user`)
|
||||
.then((data) => {
|
||||
if (data.data && data.data.username) {
|
||||
return oldRender();
|
||||
}
|
||||
localStorage.removeItem(config.authKey);
|
||||
history.push('/login');
|
||||
oldRender();
|
||||
})
|
||||
.catch((e) => {
|
||||
console.log(e);
|
||||
if (e.response.status === 401) {
|
||||
localStorage.removeItem(config.authKey);
|
||||
history.push('/login');
|
||||
oldRender();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -11,12 +11,15 @@ import { Link, history } from 'umi';
|
||||
import { LogoutOutlined } from '@ant-design/icons';
|
||||
import config from '@/utils/config';
|
||||
import 'codemirror/mode/shell/shell.js';
|
||||
import { request } from '@/utils/http';
|
||||
import './index.less';
|
||||
|
||||
export default function (props: any) {
|
||||
const logout = () => {
|
||||
localStorage.removeItem(config.authKey);
|
||||
history.push('/login');
|
||||
request.post(`${config.apiPrefix}logout`).then(() => {
|
||||
localStorage.removeItem(config.authKey);
|
||||
history.push('/login');
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
@@ -28,11 +28,6 @@ _request.interceptors.request.use((url, options) => {
|
||||
|
||||
_request.interceptors.response.use(async (response) => {
|
||||
const res = await response.clone();
|
||||
if (res.status === 401) {
|
||||
setTimeout(() => {
|
||||
history.push('/login');
|
||||
});
|
||||
}
|
||||
return response;
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user