balibabu
commited on
Commit
·
2665f5c
1
Parent(s):
b4bc2db
fix: Login with @tanstack/react-query #1306 (#1691)
Browse files### What problem does this PR solve?
fix: Login with @tanstack/react-query #1306
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
- web/src/hooks/login-hooks.ts +78 -25
- web/src/hooks/user-setting-hooks.ts +1 -16
- web/src/pages/login/index.tsx +4 -11
- web/src/pages/login/model.ts +0 -69
- web/src/pages/user-setting/sidebar/index.tsx +4 -4
- web/typings.d.ts +0 -2
web/src/hooks/login-hooks.ts
CHANGED
@@ -1,5 +1,10 @@
|
|
1 |
-
import {
|
2 |
-
import
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
export interface ILoginRequestBody {
|
5 |
email: string;
|
@@ -11,34 +16,82 @@ export interface IRegisterRequestBody extends ILoginRequestBody {
|
|
11 |
}
|
12 |
|
13 |
export const useLogin = () => {
|
14 |
-
const
|
15 |
-
|
16 |
-
const
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
},
|
24 |
-
|
25 |
-
);
|
26 |
|
27 |
-
return login;
|
28 |
};
|
29 |
|
30 |
export const useRegister = () => {
|
31 |
-
const
|
32 |
-
|
33 |
-
const
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
},
|
40 |
-
|
41 |
-
);
|
42 |
|
43 |
-
return
|
44 |
};
|
|
|
1 |
+
import { Authorization } from '@/constants/authorization';
|
2 |
+
import userService from '@/services/user-service';
|
3 |
+
import authorizationUtil from '@/utils/authorizationUtil';
|
4 |
+
import { useMutation } from '@tanstack/react-query';
|
5 |
+
import { message } from 'antd';
|
6 |
+
import { useTranslation } from 'react-i18next';
|
7 |
+
import { history } from 'umi';
|
8 |
|
9 |
export interface ILoginRequestBody {
|
10 |
email: string;
|
|
|
16 |
}
|
17 |
|
18 |
export const useLogin = () => {
|
19 |
+
const { t } = useTranslation();
|
20 |
+
|
21 |
+
const {
|
22 |
+
data,
|
23 |
+
isPending: loading,
|
24 |
+
mutateAsync,
|
25 |
+
} = useMutation({
|
26 |
+
mutationKey: ['login'],
|
27 |
+
mutationFn: async (params: { email: string; password: string }) => {
|
28 |
+
const { data: res = {}, response } = await userService.login(params);
|
29 |
+
if (res.retcode === 0) {
|
30 |
+
const { data } = res;
|
31 |
+
message.success(t('message.logged'));
|
32 |
+
const authorization = response.headers.get(Authorization);
|
33 |
+
const token = data.access_token;
|
34 |
+
const userInfo = {
|
35 |
+
avatar: data.avatar,
|
36 |
+
name: data.nickname,
|
37 |
+
email: data.email,
|
38 |
+
};
|
39 |
+
authorizationUtil.setItems({
|
40 |
+
Authorization: authorization,
|
41 |
+
userInfo: JSON.stringify(userInfo),
|
42 |
+
Token: token,
|
43 |
+
});
|
44 |
+
}
|
45 |
+
return res.retcode;
|
46 |
},
|
47 |
+
});
|
|
|
48 |
|
49 |
+
return { data, loading, login: mutateAsync };
|
50 |
};
|
51 |
|
52 |
export const useRegister = () => {
|
53 |
+
const { t } = useTranslation();
|
54 |
+
|
55 |
+
const {
|
56 |
+
data,
|
57 |
+
isPending: loading,
|
58 |
+
mutateAsync,
|
59 |
+
} = useMutation({
|
60 |
+
mutationKey: ['register'],
|
61 |
+
mutationFn: async (params: {
|
62 |
+
email: string;
|
63 |
+
password: string;
|
64 |
+
nickname: string;
|
65 |
+
}) => {
|
66 |
+
const { data = {} } = await userService.register(params);
|
67 |
+
if (data.retcode === 0) {
|
68 |
+
message.success(t('message.registered'));
|
69 |
+
}
|
70 |
+
return data.retcode;
|
71 |
+
},
|
72 |
+
});
|
73 |
+
|
74 |
+
return { data, loading, register: mutateAsync };
|
75 |
+
};
|
76 |
+
|
77 |
+
export const useLogout = () => {
|
78 |
+
const { t } = useTranslation();
|
79 |
+
const {
|
80 |
+
data,
|
81 |
+
isPending: loading,
|
82 |
+
mutateAsync,
|
83 |
+
} = useMutation({
|
84 |
+
mutationKey: ['logout'],
|
85 |
+
mutationFn: async () => {
|
86 |
+
const { data = {} } = await userService.logout();
|
87 |
+
if (data.retcode === 0) {
|
88 |
+
message.success(t('message.logout'));
|
89 |
+
authorizationUtil.removeAll();
|
90 |
+
history.push('/login');
|
91 |
+
}
|
92 |
+
return data.retcode;
|
93 |
},
|
94 |
+
});
|
|
|
95 |
|
96 |
+
return { data, loading, logout: mutateAsync };
|
97 |
};
|
web/src/hooks/user-setting-hooks.ts
CHANGED
@@ -1,9 +1,8 @@
|
|
1 |
import { ITenantInfo } from '@/interfaces/database/knowledge';
|
2 |
import { ISystemStatus, IUserInfo } from '@/interfaces/database/userSetting';
|
3 |
import userService from '@/services/user-service';
|
4 |
-
import authorizationUtil from '@/utils/authorizationUtil';
|
5 |
import { useCallback, useEffect, useMemo, useState } from 'react';
|
6 |
-
import {
|
7 |
|
8 |
export const useFetchUserInfo = () => {
|
9 |
const dispatch = useDispatch();
|
@@ -67,20 +66,6 @@ export const useSelectParserList = (): Array<{
|
|
67 |
return parserList;
|
68 |
};
|
69 |
|
70 |
-
export const useLogout = () => {
|
71 |
-
const dispatch = useDispatch(); // TODO: clear redux state
|
72 |
-
|
73 |
-
const logout = useCallback(async () => {
|
74 |
-
const retcode = await dispatch<any>({ type: 'loginModel/logout' });
|
75 |
-
if (retcode === 0) {
|
76 |
-
authorizationUtil.removeAll();
|
77 |
-
history.push('/login');
|
78 |
-
}
|
79 |
-
}, [dispatch]);
|
80 |
-
|
81 |
-
return logout;
|
82 |
-
};
|
83 |
-
|
84 |
export const useSaveSetting = () => {
|
85 |
const dispatch = useDispatch();
|
86 |
|
|
|
1 |
import { ITenantInfo } from '@/interfaces/database/knowledge';
|
2 |
import { ISystemStatus, IUserInfo } from '@/interfaces/database/userSetting';
|
3 |
import userService from '@/services/user-service';
|
|
|
4 |
import { useCallback, useEffect, useMemo, useState } from 'react';
|
5 |
+
import { useDispatch, useSelector } from 'umi';
|
6 |
|
7 |
export const useFetchUserInfo = () => {
|
8 |
const dispatch = useDispatch();
|
|
|
66 |
return parserList;
|
67 |
};
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
export const useSaveSetting = () => {
|
70 |
const dispatch = useDispatch();
|
71 |
|
web/src/pages/login/index.tsx
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import { useLogin, useRegister } from '@/hooks/login-hooks';
|
2 |
-
import { useOneNamespaceEffectsLoading } from '@/hooks/store-hooks';
|
3 |
import { rsaPsw } from '@/utils';
|
4 |
import { Button, Checkbox, Form, Input } from 'antd';
|
5 |
import { useEffect, useState } from 'react';
|
@@ -13,16 +12,10 @@ import styles from './index.less';
|
|
13 |
const Login = () => {
|
14 |
const [title, setTitle] = useState('login');
|
15 |
const navigate = useNavigate();
|
16 |
-
const login = useLogin();
|
17 |
-
const register = useRegister();
|
18 |
const { t } = useTranslation('translation', { keyPrefix: 'login' });
|
19 |
-
|
20 |
-
// TODO: When the server address request is not accessible, the value of dva-loading always remains true.
|
21 |
-
|
22 |
-
const signLoading = useOneNamespaceEffectsLoading('loginModel', [
|
23 |
-
'login',
|
24 |
-
'register',
|
25 |
-
]);
|
26 |
|
27 |
const changeTitle = () => {
|
28 |
setTitle((title) => (title === 'login' ? 'register' : 'login'));
|
@@ -148,7 +141,7 @@ const Login = () => {
|
|
148 |
block
|
149 |
size="large"
|
150 |
onClick={onCheck}
|
151 |
-
loading={
|
152 |
>
|
153 |
{title === 'login' ? t('login') : t('continue')}
|
154 |
</Button>
|
|
|
1 |
import { useLogin, useRegister } from '@/hooks/login-hooks';
|
|
|
2 |
import { rsaPsw } from '@/utils';
|
3 |
import { Button, Checkbox, Form, Input } from 'antd';
|
4 |
import { useEffect, useState } from 'react';
|
|
|
12 |
const Login = () => {
|
13 |
const [title, setTitle] = useState('login');
|
14 |
const navigate = useNavigate();
|
15 |
+
const { login, loading: signLoading } = useLogin();
|
16 |
+
const { register, loading: registerLoading } = useRegister();
|
17 |
const { t } = useTranslation('translation', { keyPrefix: 'login' });
|
18 |
+
const loading = signLoading || registerLoading;
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
const changeTitle = () => {
|
21 |
setTitle((title) => (title === 'login' ? 'register' : 'login'));
|
|
|
141 |
block
|
142 |
size="large"
|
143 |
onClick={onCheck}
|
144 |
+
loading={loading}
|
145 |
>
|
146 |
{title === 'login' ? t('login') : t('continue')}
|
147 |
</Button>
|
web/src/pages/login/model.ts
DELETED
@@ -1,69 +0,0 @@
|
|
1 |
-
import { Authorization } from '@/constants/authorization';
|
2 |
-
import i18n from '@/locales/config';
|
3 |
-
import userService from '@/services/user-service';
|
4 |
-
import authorizationUtil from '@/utils/authorizationUtil';
|
5 |
-
import { message } from 'antd';
|
6 |
-
import { DvaModel } from 'umi';
|
7 |
-
|
8 |
-
export interface LoginModelState {
|
9 |
-
list: any[];
|
10 |
-
info: any;
|
11 |
-
visible: boolean;
|
12 |
-
}
|
13 |
-
|
14 |
-
const model: DvaModel<LoginModelState> = {
|
15 |
-
namespace: 'loginModel',
|
16 |
-
state: {
|
17 |
-
list: [],
|
18 |
-
info: {},
|
19 |
-
visible: false,
|
20 |
-
},
|
21 |
-
reducers: {
|
22 |
-
updateState(state, { payload }) {
|
23 |
-
return {
|
24 |
-
...state,
|
25 |
-
...payload,
|
26 |
-
};
|
27 |
-
},
|
28 |
-
},
|
29 |
-
effects: {
|
30 |
-
*login({ payload = {} }, { call }) {
|
31 |
-
const { data, response } = yield call(userService.login, payload);
|
32 |
-
const { retcode, data: res } = data;
|
33 |
-
const authorization = response.headers.get(Authorization);
|
34 |
-
if (retcode === 0) {
|
35 |
-
message.success(i18n.t('message.logged'));
|
36 |
-
const token = res.access_token;
|
37 |
-
const userInfo = {
|
38 |
-
avatar: res.avatar,
|
39 |
-
name: res.nickname,
|
40 |
-
email: res.email,
|
41 |
-
};
|
42 |
-
authorizationUtil.setItems({
|
43 |
-
Authorization: authorization,
|
44 |
-
userInfo: JSON.stringify(userInfo),
|
45 |
-
Token: token,
|
46 |
-
});
|
47 |
-
}
|
48 |
-
return retcode;
|
49 |
-
},
|
50 |
-
*register({ payload = {} }, { call }) {
|
51 |
-
const { data } = yield call(userService.register, payload);
|
52 |
-
console.log();
|
53 |
-
const { retcode } = data;
|
54 |
-
if (retcode === 0) {
|
55 |
-
message.success(i18n.t('message.registered'));
|
56 |
-
}
|
57 |
-
return retcode;
|
58 |
-
},
|
59 |
-
*logout({ payload = {} }, { call }) {
|
60 |
-
const { data } = yield call(userService.logout, payload);
|
61 |
-
const { retcode } = data;
|
62 |
-
if (retcode === 0) {
|
63 |
-
message.success(i18n.t('message.logout'));
|
64 |
-
}
|
65 |
-
return retcode;
|
66 |
-
},
|
67 |
-
},
|
68 |
-
};
|
69 |
-
export default model;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
web/src/pages/user-setting/sidebar/index.tsx
CHANGED
@@ -1,5 +1,8 @@
|
|
1 |
import { Domain } from '@/constants/common';
|
|
|
|
|
2 |
import { useSecondPathName } from '@/hooks/route-hook';
|
|
|
3 |
import type { MenuProps } from 'antd';
|
4 |
import { Flex, Menu } from 'antd';
|
5 |
import React, { useEffect, useMemo } from 'react';
|
@@ -9,9 +12,6 @@ import {
|
|
9 |
UserSettingIconMap,
|
10 |
UserSettingRouteKey,
|
11 |
} from '../constants';
|
12 |
-
|
13 |
-
import { useTranslate } from '@/hooks/common-hooks';
|
14 |
-
import { useFetchSystemVersion, useLogout } from '@/hooks/user-setting-hooks';
|
15 |
import styles from './index.less';
|
16 |
|
17 |
type MenuItem = Required<MenuProps>['items'][number];
|
@@ -19,7 +19,7 @@ type MenuItem = Required<MenuProps>['items'][number];
|
|
19 |
const SideBar = () => {
|
20 |
const navigate = useNavigate();
|
21 |
const pathName = useSecondPathName();
|
22 |
-
const logout = useLogout();
|
23 |
const { t } = useTranslate('setting');
|
24 |
const { version, fetchSystemVersion } = useFetchSystemVersion();
|
25 |
|
|
|
1 |
import { Domain } from '@/constants/common';
|
2 |
+
import { useTranslate } from '@/hooks/common-hooks';
|
3 |
+
import { useLogout } from '@/hooks/login-hooks';
|
4 |
import { useSecondPathName } from '@/hooks/route-hook';
|
5 |
+
import { useFetchSystemVersion } from '@/hooks/user-setting-hooks';
|
6 |
import type { MenuProps } from 'antd';
|
7 |
import { Flex, Menu } from 'antd';
|
8 |
import React, { useEffect, useMemo } from 'react';
|
|
|
12 |
UserSettingIconMap,
|
13 |
UserSettingRouteKey,
|
14 |
} from '../constants';
|
|
|
|
|
|
|
15 |
import styles from './index.less';
|
16 |
|
17 |
type MenuItem = Required<MenuProps>['items'][number];
|
|
|
19 |
const SideBar = () => {
|
20 |
const navigate = useNavigate();
|
21 |
const pathName = useSecondPathName();
|
22 |
+
const { logout } = useLogout();
|
23 |
const { t } = useTranslate('setting');
|
24 |
const { version, fetchSystemVersion } = useFetchSystemVersion();
|
25 |
|
web/typings.d.ts
CHANGED
@@ -3,7 +3,6 @@ import { KFModelState } from '@/pages/add-knowledge/components/knowledge-file/mo
|
|
3 |
import { TestingModelState } from '@/pages/add-knowledge/components/knowledge-testing/model';
|
4 |
import { kAModelState } from '@/pages/add-knowledge/model';
|
5 |
import { ChatModelState } from '@/pages/chat/model';
|
6 |
-
import { LoginModelState } from '@/pages/login/model';
|
7 |
import { SettingModelState } from '@/pages/user-setting/model';
|
8 |
|
9 |
declare module 'lodash';
|
@@ -15,7 +14,6 @@ function useSelector<TState = RootState, TSelected = unknown>(
|
|
15 |
|
16 |
export interface RootState {
|
17 |
chatModel: ChatModelState;
|
18 |
-
loginModel: LoginModelState;
|
19 |
settingModel: SettingModelState;
|
20 |
kFModel: KFModelState;
|
21 |
kAModel: kAModelState;
|
|
|
3 |
import { TestingModelState } from '@/pages/add-knowledge/components/knowledge-testing/model';
|
4 |
import { kAModelState } from '@/pages/add-knowledge/model';
|
5 |
import { ChatModelState } from '@/pages/chat/model';
|
|
|
6 |
import { SettingModelState } from '@/pages/user-setting/model';
|
7 |
|
8 |
declare module 'lodash';
|
|
|
14 |
|
15 |
export interface RootState {
|
16 |
chatModel: ChatModelState;
|
|
|
17 |
settingModel: SettingModelState;
|
18 |
kFModel: KFModelState;
|
19 |
kAModel: kAModelState;
|