Skip to content

Commit bcfe009

Browse files
committed
protected routes
1 parent 47a3097 commit bcfe009

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/shared/components/Contentful/Route.jsx

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import React from 'react';
1313
import { Route, Switch, Redirect } from 'react-router-dom';
1414
import Viewport from 'components/Contentful/Viewport';
1515
import PasswordScreen from 'components/Contentful/PasswordScreen';
16-
import { isomorphy } from 'topcoder-react-utils';
16+
import { isomorphy, config } from 'topcoder-react-utils';
17+
import { connect } from 'react-redux';
1718
import { removeTrailingSlash } from 'utils/url';
1819

1920
// Concatenates a base and segment and handles optional trailing slashes
@@ -142,7 +143,7 @@ RedirectWithStatus.propTypes = {
142143
status: PT.number.isRequired,
143144
};
144145

145-
export default function ContentfulRoute(props) {
146+
function ContentfulRoute(props) {
146147
const {
147148
baseUrl,
148149
error404,
@@ -152,6 +153,7 @@ export default function ContentfulRoute(props) {
152153
preview,
153154
spaceName,
154155
environment,
156+
auth,
155157
} = props;
156158

157159
const queries = [];
@@ -173,6 +175,13 @@ export default function ContentfulRoute(props) {
173175
// eslint-disable-next-line no-restricted-globals
174176
const currentPathname = typeof location === 'undefined' ? '' : removeTrailingSlash(location.pathname);
175177
const redirectToUrl = _.trim(fields.redirectToUrl);
178+
const requireLogin = fields.protected;
179+
if (requireLogin && (!auth || !auth.tokenV3)) {
180+
// route is protected by TC Login
181+
// send to login/register with proper retUrl set
182+
const authUrl = config.URL.AUTH;
183+
return <RedirectWithStatus status={401} from={url} to={`${authUrl}?retUrl=${encodeURIComponent(url)}`} />
184+
}
176185
return redirectToUrl && currentPathname === url ? (
177186
<RedirectWithStatus status={301} from={url} to={redirectToUrl} />
178187
) : (
@@ -207,6 +216,7 @@ ContentfulRoute.defaultProps = {
207216
preview: false,
208217
spaceName: null,
209218
environment: null,
219+
auth: null,
210220
};
211221

212222
ContentfulRoute.propTypes = {
@@ -218,4 +228,16 @@ ContentfulRoute.propTypes = {
218228
preview: PT.bool,
219229
spaceName: PT.string,
220230
environment: PT.string,
231+
auth: PT.shape(),
221232
};
233+
234+
function mapStateToProps(state) {
235+
const auth = state.auth && state.auth.profile ? { ...state.auth } : null;
236+
return {
237+
auth,
238+
};
239+
}
240+
241+
export default connect(
242+
mapStateToProps,
243+
)(ContentfulRoute);

0 commit comments

Comments
 (0)