Skip to content

Commit

Permalink
fix: Encode referrer to handle complex paths
Browse files Browse the repository at this point in the history
  • Loading branch information
josebui committed Oct 3, 2023
1 parent 13a7396 commit c094b03
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/account/components/AccountLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import React from 'react';
import { Trans, useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
import { useSearchParams } from 'react-router-dom';
import { Navigate, useSearchParams } from 'react-router-dom';
import { fetchAuthURLs } from 'terraso-client-shared/account/accountSlice';
import { useFetchData } from 'terraso-client-shared/store/utils';
import AppleIcon from '@mui/icons-material/Apple';
Expand All @@ -44,14 +44,16 @@ const GoogleIcon = props => {
return <SvgIcon component={GoogleSvg} {...props} />;
};

const appendReferrer = (url, referrer) =>
referrer ? `${url}&state=${referrer}` : url;
const appendReferrer = (url, referrer) => {
return referrer ? `${url}&state=/account?referrer=${referrer}` : url;
};

const AccountForm = () => {
const { t } = useTranslation();
const { trackEvent } = useAnalytics();
const [searchParams] = useSearchParams();
const { fetching, urls } = useSelector(state => state.account.login);
const hasToken = useSelector(state => state.account.hasToken);
const referrer = searchParams.get('referrer');

useDocumentTitle(t('account.login_document_title'));
Expand All @@ -63,6 +65,10 @@ const AccountForm = () => {
return <PageLoader />;
}

if (hasToken) {
return <Navigate to={referrer ? atob(referrer) : '/'} replace />;
}

return (
<Stack
direction="column"
Expand Down
5 changes: 4 additions & 1 deletion src/navigation/navigationUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ export const generateReferrerPath = location => {
const referrer = [path.substring(1), queryParams]
.filter(part => part)
.join('');
return referrer;
if (!referrer) {
return null;
}
return btoa(`/${referrer}`);
};

0 comments on commit c094b03

Please sign in to comment.