Skip to content

Commit

Permalink
extract logic
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisamir98 committed Sep 28, 2023
1 parent 95ef031 commit af613df
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
12 changes: 6 additions & 6 deletions src/script/auth/page/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import {bindActionCreators, RootState} from '../module/reducer';
import * as AuthSelector from '../module/selector/AuthSelector';
import {QUERY_KEY, ROUTE} from '../route';
import {parseError, parseValidationErrors} from '../util/errorUtil';
import {getOAuthQueryString} from '../util/oauthUtil';

type Props = React.HTMLProps<HTMLDivElement> & {
embedded?: boolean;
Expand Down Expand Up @@ -180,11 +181,10 @@ const LoginComponent = ({
await doInit({isImmediateLogin: true, shouldValidateLocalClient: false});
const entropyData = await getEntropy?.();
await doInitializeClient(ClientType.PERMANENT, undefined, undefined, entropyData);
const hash = window.location.hash;
const queryParamIndex = hash.indexOf('?');

if (isOauth) {
return navigate(`${ROUTE.AUTHORIZE}/${hash.substring(queryParamIndex)}`);
const queryString = getOAuthQueryString(window.location);
return navigate(`${ROUTE.AUTHORIZE}/${queryString}`);
}
return navigate(ROUTE.HISTORY_INFO);
} catch (error) {
Expand All @@ -208,9 +208,9 @@ const LoginComponent = ({
await doLogin(login, getEntropy);

if (isOauth) {
const hash = window.location.hash;
const queryParamIndex = hash.indexOf('?');
return navigate(`${ROUTE.AUTHORIZE}/${hash.substring(queryParamIndex)}`);
const queryString = getOAuthQueryString(window.location);

return navigate(`${ROUTE.AUTHORIZE}/${queryString}`);
}
return navigate(ROUTE.HISTORY_INFO);
} catch (error) {
Expand Down
11 changes: 3 additions & 8 deletions src/script/auth/page/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {bindActionCreators, RootState} from '../module/reducer';
import * as AuthSelector from '../module/selector/AuthSelector';
import * as LanguageSelector from '../module/selector/LanguageSelector';
import {ROUTE} from '../route';
import {getOAuthQueryString} from '../util/oauthUtil';

interface RootProps {}

Expand Down Expand Up @@ -115,14 +116,8 @@ const RootComponent: FC<RootProps & ConnectedProps & DispatchProps> = ({
return page;
}

const hash = window.location.hash;
const queryParamIndex = hash.indexOf('?');

if (hash && queryParamIndex > 0) {
return <Navigate to={`${ROUTE.LOGIN}/${hash.substring(queryParamIndex)}`} />;
}

return <Navigate to={ROUTE.LOGIN} />;
const queryString = getOAuthQueryString(window.location);
return queryString ? <Navigate to={`${ROUTE.LOGIN}/${queryString}`} /> : <Navigate to={ROUTE.LOGIN} />;
}
return null;
};
Expand Down
11 changes: 8 additions & 3 deletions src/script/auth/util/oauthUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ import {Scope} from '../page/OAuthPermissions';
* @returns OAuthBody
*/
export const oAuthParams = (location: Location) => {
const hash = window.location.hash;
const queryParamIndex = hash.indexOf('?');
const params = new URLSearchParams(queryParamIndex > 0 ? hash.substring(queryParamIndex) : location.hash);
const queryString = getOAuthQueryString(location);
const params = new URLSearchParams(queryString ?? location.hash);
return Object.fromEntries(params) as unknown as OAuthBody;
};

export const getOAuthQueryString = (location: Location) => {
const hash = location.hash;
const queryParamIndex = hash.indexOf('?');
return hash.substring(queryParamIndex);
};

/**
* Takes the oauth body and returns the scopes as an array of Scopes accepted by the app.
* @param oauthBody oauth body object
Expand Down

0 comments on commit af613df

Please sign in to comment.