Skip to content

Commit 8ee4e1e

Browse files
authored
Merge pull request #481 from raheeliftikhar5/oauth-app-reload-fix
Avoid reloading app after login/register + remove calling same api requests multiple times.
2 parents e8bf78e + 4971e09 commit 8ee4e1e

File tree

9 files changed

+44
-54
lines changed

9 files changed

+44
-54
lines changed

client/packages/lowcoder/src/app.tsx

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,13 @@ import { CodeEditorTooltipContainer } from "base/codeEditor/codeEditor";
3939
import { ProductLoading } from "components/ProductLoading";
4040
import { language, trans } from "i18n";
4141
import { loadComps } from "comps";
42-
import { fetchHomeData } from "redux/reduxActions/applicationActions";
4342
import { initApp } from "util/commonUtils";
4443
import ApplicationHome from "./pages/ApplicationV2";
4544
import { favicon } from "@lowcoder-ee/assets/images";
4645
import { hasQueryParam } from "util/urlUtils";
4746
import { isFetchUserFinished } from "redux/selectors/usersSelectors";
4847
import { SystemWarning } from "./components/SystemWarning";
49-
import { getBrandingConfig, getSystemConfigFetching } from "./redux/selectors/configSelectors";
48+
import { getBrandingConfig } from "./redux/selectors/configSelectors";
5049
import { buildMaterialPreviewURL } from "./util/materialUtils";
5150
import GlobalInstances from 'components/GlobalInstances';
5251

@@ -71,35 +70,22 @@ const Wrapper = (props: { children: React.ReactNode }) => (
7170

7271
type AppIndexProps = {
7372
isFetchUserFinished: boolean;
74-
isFetchHomeFinished: boolean;
75-
// isFetchingConfig: boolean;
7673
currentOrgId?: string;
7774
orgDev: boolean;
7875
defaultHomePage: string | null | undefined;
7976
fetchConfig: (orgId?: string) => void;
8077
getCurrentUser: () => void;
81-
fetchHome: () => void;
8278
favicon: string;
8379
brandName: string;
8480
};
8581

8682
class AppIndex extends React.Component<AppIndexProps, any> {
8783
componentDidMount() {
8884
this.props.getCurrentUser();
89-
const { pathname } = history.location;
90-
91-
this.props.fetchConfig(this.props.currentOrgId);
92-
93-
if (pathname === BASE_URL) {
94-
this.props.fetchHome();
95-
}
9685
}
9786

9887
componentDidUpdate(prevProps: AppIndexProps) {
99-
if (history.location.pathname === BASE_URL) {
100-
this.props.fetchHome();
101-
}
102-
if(prevProps.currentOrgId !== this.props.currentOrgId) {
88+
if(prevProps.currentOrgId !== this.props.currentOrgId && this.props.currentOrgId !== '') {
10389
this.props.fetchConfig(this.props.currentOrgId);
10490
}
10591
}
@@ -108,11 +94,7 @@ class AppIndex extends React.Component<AppIndexProps, any> {
10894
const isTemplate = hasQueryParam("template");
10995
const pathname = history.location.pathname;
11096
// make sure all users in this app have checked login info
111-
if (
112-
!this.props.isFetchUserFinished ||
113-
// this.props.isFetchingConfig ||
114-
(pathname === BASE_URL && !this.props.isFetchHomeFinished)
115-
) {
97+
if (!this.props.isFetchUserFinished) {
11698
const hideLoadingHeader = isTemplate || isAuthUnRequired(pathname);
11799
return <ProductLoading hideHeader={hideLoadingHeader} />;
118100
}
@@ -185,11 +167,9 @@ class AppIndex extends React.Component<AppIndexProps, any> {
185167

186168
const mapStateToProps = (state: AppState) => ({
187169
isFetchUserFinished: isFetchUserFinished(state),
188-
// isFetchingConfig: getSystemConfigFetching(state),
189170
orgDev: state.ui.users.user.orgDev,
190171
currentOrgId: state.ui.users.user.currentOrgId,
191172
defaultHomePage: state.ui.application.homeOrg?.commonSettings.defaultHomePage,
192-
isFetchHomeFinished: state.ui.application.loadingStatus.fetchHomeDataFinished,
193173
favicon: getBrandingConfig(state)?.favicon
194174
? buildMaterialPreviewURL(getBrandingConfig(state)?.favicon!)
195175
: favicon,
@@ -201,7 +181,6 @@ const mapDispatchToProps = (dispatch: any) => ({
201181
dispatch(fetchUserAction());
202182
},
203183
fetchConfig: (orgId?: string) => dispatch(fetchConfigAction(orgId)),
204-
fetchHome: () => dispatch(fetchHomeData({})),
205184
});
206185

207186
const AppIndexWithProps = connect(mapStateToProps, mapDispatchToProps)(AppIndex);

client/packages/lowcoder/src/pages/ApplicationV2/HomeView.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
import { useDispatch, useSelector } from "react-redux";
1+
import { useSelector } from "react-redux";
22
import { HomeLayout } from "./HomeLayout";
3-
import { useEffect } from "react";
4-
import { fetchFolderElements } from "../../redux/reduxActions/folderActions";
53
import { getUser } from "../../redux/selectors/usersSelectors";
64
import { folderElementsSelector } from "../../redux/selectors/folderSelector";
75

86
export function HomeView() {
9-
const dispatch = useDispatch();
10-
117
const elements = useSelector(folderElementsSelector)[""];
128
const user = useSelector(getUser);
139

14-
useEffect(() => {
15-
dispatch(fetchFolderElements({}));
16-
}, []);
17-
1810
if (!user.currentOrgId) {
1911
return null;
2012
}

client/packages/lowcoder/src/pages/userAuth/authUtils.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ import {
2121
ThirdPartyAuthType,
2222
ThirdPartyConfigType,
2323
} from "constants/authConstants";
24+
import history from "util/history";
2425

2526
export const AuthContext = createContext<{
2627
systemConfig?: SystemConfig;
2728
inviteInfo?: AuthInviteInfo;
2829
thirdPartyAuthError?: boolean;
30+
fetchUserAfterAuthSuccess?: () => void;
2931
}>(undefined as any);
3032

3133
export const getSafeAuthRedirectURL = (redirectUrl: string | null) => {
@@ -39,15 +41,21 @@ export const getSafeAuthRedirectURL = (redirectUrl: string | null) => {
3941
export function useAuthSubmit(
4042
requestFunc: () => AxiosPromise<ApiResponse>,
4143
infoCompleteCheck: boolean,
42-
redirectUrl: string | null
44+
redirectUrl: string | null,
45+
onAuthSuccess?: () => void,
4346
) {
4447
const [loading, setLoading] = useState(false);
4548
return {
4649
loading: loading,
4750
onSubmit: () => {
4851
setLoading(true);
4952
requestFunc()
50-
.then((resp) => authRespValidate(resp, infoCompleteCheck, redirectUrl))
53+
.then((resp) => authRespValidate(
54+
resp,
55+
infoCompleteCheck,
56+
redirectUrl,
57+
onAuthSuccess,
58+
))
5159
.catch((e) => {
5260
messageInstance.error(e.message);
5361
})
@@ -66,7 +74,8 @@ export function useAuthSubmit(
6674
export function authRespValidate(
6775
resp: AxiosResponse<ApiResponse>,
6876
infoCompleteCheck: boolean,
69-
redirectUrl: string | null
77+
redirectUrl: string | null,
78+
onAuthSuccess?: () => void
7079
) {
7180
let replaceUrl = redirectUrl || BASE_URL;
7281
if (infoCompleteCheck) {
@@ -76,7 +85,8 @@ export function authRespValidate(
7685
: USER_INFO_COMPLETION;
7786
}
7887
if (doValidResponse(resp)) {
79-
window.location.replace(replaceUrl);
88+
onAuthSuccess?.();
89+
history.replace(replaceUrl);
8090
} else if (
8191
resp.data.code === SERVER_ERROR_CODES.EXCEED_MAX_USER_ORG_COUNT ||
8292
resp.data.code === SERVER_ERROR_CODES.ALREADY_IN_ORGANIZATION

client/packages/lowcoder/src/pages/userAuth/formLogin.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default function FormLogin(props: FormLoginProps) {
3232
const [account, setAccount] = useState("");
3333
const [password, setPassword] = useState("");
3434
const redirectUrl = useRedirectUrl();
35-
const { systemConfig, inviteInfo } = useContext(AuthContext);
35+
const { systemConfig, inviteInfo, fetchUserAfterAuthSuccess } = useContext(AuthContext);
3636
const invitationId = inviteInfo?.invitationId;
3737
const authId = systemConfig?.form.id;
3838
const location = useLocation();
@@ -49,7 +49,8 @@ export default function FormLogin(props: FormLoginProps) {
4949
authId,
5050
}),
5151
false,
52-
redirectUrl
52+
redirectUrl,
53+
fetchUserAfterAuthSuccess,
5354
);
5455

5556
return (

client/packages/lowcoder/src/pages/userAuth/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { AuthRoutes } from "@lowcoder-ee/constants/authConstants";
88
import { AuthLocationState } from "constants/authConstants";
99
import { ProductLoading } from "components/ProductLoading";
1010
import { fetchConfigAction } from "redux/reduxActions/configActions";
11+
import { fetchUserAction } from "redux/reduxActions/userActions";
1112
import _ from "lodash";
1213

1314
export default function UserAuth() {
@@ -34,12 +35,17 @@ export default function UserAuth() {
3435
return <ProductLoading hideHeader />;
3536
}
3637

38+
const fetchUserAfterAuthSuccess = () => {
39+
dispatch(fetchUserAction());
40+
}
41+
3742
return (
3843
<AuthContext.Provider
3944
value={{
4045
systemConfig: systemConfig,
4146
inviteInfo: location.state?.inviteInfo,
4247
thirdPartyAuthError: location.state?.thirdPartyAuthError,
48+
fetchUserAfterAuthSuccess,
4349
}}
4450
>
4551
<Switch location={location}>

client/packages/lowcoder/src/pages/userAuth/login.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function Login() {
144144
heading={loginHeading}
145145
subHeading={loginSubHeading}
146146
>
147-
{loginCardView}
147+
<FormLogin organizationId={organizationId} />
148148
</AuthContainer>
149149
);
150150
}

client/packages/lowcoder/src/pages/userAuth/register.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ function UserRegister() {
4141
const [password, setPassword] = useState("");
4242
const redirectUrl = useRedirectUrl();
4343
const location = useLocation();
44-
const { systemConfig, inviteInfo } = useContext(AuthContext);
44+
const { systemConfig, inviteInfo, fetchUserAfterAuthSuccess } = useContext(AuthContext);
4545
const invitationId = inviteInfo?.invitationId;
46-
// const invitedOrganizationId = inviteInfo?.invitedOrganizationId;
46+
4747
const orgId = useParams<any>().orgId;
4848
const organizationId = useMemo(() => {
4949
if(inviteInfo?.invitedOrganizationId) {
@@ -53,6 +53,7 @@ function UserRegister() {
5353
}, [ inviteInfo, orgId ])
5454

5555
const authId = systemConfig?.form.id;
56+
5657
const { loading, onSubmit } = useAuthSubmit(
5758
() =>
5859
UserApi.formLogin({
@@ -64,13 +65,10 @@ function UserRegister() {
6465
authId,
6566
}),
6667
false,
67-
redirectUrl
68+
redirectUrl,
69+
fetchUserAfterAuthSuccess,
6870
);
6971

70-
if (!systemConfig || !systemConfig?.form.enableRegister) {
71-
return null;
72-
}
73-
7472
const registerHeading = REACT_APP_LOWCODER_CUSTOM_AUTH_WELCOME_TEXT !== ""
7573
? REACT_APP_LOWCODER_CUSTOM_AUTH_WELCOME_TEXT
7674
: trans("userAuth.register")

client/packages/lowcoder/src/pages/userAuth/thirdParty/authRedirect.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import { AUTH_LOGIN_URL, AUTH_REGISTER_URL, BASE_URL } from "constants/routesURL
66
import history from "util/history";
77
import PageSkeleton from "components/PageSkeleton";
88
import { trans } from "i18n";
9-
import { useEffect, useState } from "react";
9+
import { useContext, useEffect, useState } from "react";
1010
import { getAuthenticator } from "@lowcoder-ee/pages/userAuth/thirdParty/authenticator";
1111
import { AuthRedirectUrlParams } from "pages/userAuth/thirdParty/authenticator";
12-
import { loadAuthParams } from "pages/userAuth/authUtils";
12+
import { AuthContext, loadAuthParams } from "pages/userAuth/authUtils";
1313

1414
function getUrlParams(queryParams: URLSearchParams): AuthRedirectUrlParams {
1515
const ticket = queryParams.get("ticket");
@@ -53,6 +53,8 @@ export function AuthRedirect() {
5353
const queryParams = new URLSearchParams(location.search);
5454
const urlParam = getUrlParams(queryParams);
5555
const [authParams, setAuthParam] = useState<AuthSessionStoreParams>();
56+
const { fetchUserAfterAuthSuccess } = useContext(AuthContext);
57+
5658
useEffect(() => {
5759
const localAuthParams = loadAuthParams();
5860
if (!localAuthParams) {
@@ -61,8 +63,9 @@ export function AuthRedirect() {
6163
setAuthParam(localAuthParams);
6264
}
6365
}, []);
66+
6467
if (authParams && validateParam(authParams, urlParam)) {
65-
getAuthenticator(authParams, urlParam).doAuth();
68+
getAuthenticator(authParams, urlParam).doAuth(fetchUserAfterAuthSuccess);
6669
}
6770
return <PageSkeleton hideSideBar />;
6871
}

client/packages/lowcoder/src/pages/userAuth/thirdParty/authenticator/abstractAuthenticator.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,21 @@ export abstract class AbstractAuthenticator {
2626
this.redirectUrl = decodeURIComponent(getRedirectUrl(authParams.authType));
2727
}
2828

29-
doAuth() {
29+
doAuth(onAuthSuccess?: () => void) {
3030
const { authParams } = this;
3131
(authParams.authGoal === "login" || authParams.authGoal === "register")
32-
? this.doLogin()
32+
? this.doLogin(onAuthSuccess)
3333
: this.doBind();
3434
}
3535

36-
protected doLogin() {
36+
protected doLogin(onAuthSuccess?: () => void) {
3737
this.login()
3838
.then((resp) => {
3939
authRespValidate(
4040
resp,
4141
this.needInfoCheck(this.authParams.sourceType),
42-
this.authParams.afterLoginRedirect
42+
this.authParams.afterLoginRedirect,
43+
onAuthSuccess,
4344
);
4445
})
4546
.catch((e) => {

0 commit comments

Comments
 (0)