Skip to content

Commit ae91db0

Browse files
refactor: handle redirectUrl using sessionStoragee
1 parent 5dd8d32 commit ae91db0

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

client/packages/lowcoder/src/constants/authConstants.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ import {
2222
export type AuthInviteInfo = InviteInfo & { invitationId: string };
2323
export type AuthLocationState = { inviteInfo?: AuthInviteInfo; thirdPartyAuthError?: boolean };
2424

25-
export const AuthSearchParams = {
26-
loginType: "loginType",
27-
redirectUrl: "redirectUrl",
25+
export enum AuthSearchParams {
26+
loginType = "loginType",
27+
redirectUrl = "redirectUrl",
28+
};
29+
30+
export type AuthSearchParamsType = {
31+
loginType: string | null,
32+
redirectUrl: string | null,
2833
};
2934

3035
export type OauthRequestParam = {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { createContext, useState } from "react";
1616
import { SystemConfig } from "constants/configConstants";
1717
import {
1818
AuthInviteInfo,
19+
AuthSearchParamsType,
1920
AuthSessionStoreParams,
2021
ThirdPartyAuthGoal,
2122
ThirdPartyAuthType,
@@ -185,3 +186,21 @@ export const getRedirectUrl = (authType: ThirdPartyAuthType) => {
185186
`${window.location.origin}${authType === "CAS" ? CAS_AUTH_REDIRECT : OAUTH_REDIRECT}`
186187
);
187188
};
189+
190+
const AuthSearchParamStorageKey = "_temp_auth_search_params_";
191+
192+
export const saveAuthSearchParams = (
193+
authSearchParams: AuthSearchParamsType
194+
) => {
195+
sessionStorage.setItem(AuthSearchParamStorageKey, JSON.stringify(authSearchParams));
196+
}
197+
198+
export const loadAuthSearchParams = ():AuthSearchParamsType | null => {
199+
const authParams = sessionStorage.getItem(AuthSearchParamStorageKey);
200+
if (!authParams) return null;
201+
return JSON.parse(authParams);
202+
}
203+
204+
export const clearAuthSearchParams = () => {
205+
sessionStorage.removeItem(AuthSearchParamStorageKey);
206+
}

client/packages/lowcoder/src/redux/sagas/userSagas.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { defaultUser } from "constants/userConstants";
2323
import { messageInstance } from "lowcoder-design";
2424

2525
import { AuthSearchParams } from "constants/authConstants";
26+
import { saveAuthSearchParams } from "pages/userAuth/authUtils";
2627

2728
function validResponseData(response: AxiosResponse<ApiResponse>) {
2829
return response && response.data && response.data.data;
@@ -133,13 +134,18 @@ export function* logoutSaga(action: LogoutActionType) {
133134
let redirectURL = AUTH_LOGIN_URL;
134135
if (action.payload.notAuthorised) {
135136
const currentUrl = window.location.href;
136-
redirectURL = `${AUTH_LOGIN_URL}?redirectUrl=${encodeURIComponent(currentUrl)}`;
137+
// redirectURL = `${AUTH_LOGIN_URL}`;
138+
// redirectURL = `${AUTH_LOGIN_URL}?redirectUrl=${encodeURIComponent(currentUrl)}`;
137139
const urlObj = new URL(currentUrl);
138140
// Add loginType param for auto login jump
139141
const loginType = urlObj.searchParams.get(AuthSearchParams.loginType);
140-
if (loginType) {
141-
redirectURL = redirectURL + `&${AuthSearchParams.loginType}=${loginType}`;
142-
}
142+
// if (loginType) {
143+
// redirectURL = redirectURL + `&${AuthSearchParams.loginType}=${loginType}`;
144+
// }
145+
saveAuthSearchParams({
146+
[AuthSearchParams.redirectUrl]: encodeURIComponent(currentUrl),
147+
[AuthSearchParams.loginType]: loginType
148+
});
143149
}
144150
let isValidResponse = true;
145151
if (!action.payload.notAuthorised) {

client/packages/lowcoder/src/util/hooks.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { checkIsMobile } from "util/commonUtils";
1616
import { EditorContext } from "comps/editorState";
1717
import { getDataSourceStructures } from "redux/selectors/datasourceSelectors";
1818
import { DatasourceStructure } from "api/datasourceApi";
19+
import { loadAuthSearchParams } from "pages/userAuth/authUtils";
1920

2021
export const ForceViewModeContext = React.createContext<boolean>(false);
2122

@@ -59,9 +60,12 @@ export function useApplicationId() {
5960
}
6061

6162
export function useRedirectUrl() {
62-
const location = useLocation();
63-
const queryParams = new URLSearchParams(location.search);
64-
return queryParams.get(AuthSearchParams.redirectUrl);
63+
const authSearchParams = loadAuthSearchParams()
64+
const redirectUrl = authSearchParams && authSearchParams.redirectUrl
65+
return redirectUrl && decodeURIComponent(redirectUrl);
66+
// const location = useLocation();
67+
// const queryParams = new URLSearchParams(location.search);
68+
// return queryParams.get(AuthSearchParams.redirectUrl);
6569
}
6670

6771
export function useFixedDelay(callback: () => Promise<unknown>, delay: number | null) {

0 commit comments

Comments
 (0)