Skip to content

Commit e5bc573

Browse files
fix: avoid reloading app after login
1 parent 32d9889 commit e5bc573

File tree

7 files changed

+40
-21
lines changed

7 files changed

+40
-21
lines changed

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)