From 3b1ccd378e633362b92ac5c378cd5850d605c657 Mon Sep 17 00:00:00 2001 From: unclekingpin Date: Thu, 20 Jul 2023 14:12:30 -0700 Subject: [PATCH] prevent authenticated user to access the Intro route --- src/App/App.js | 7 +++-- src/App/withProtectedRoutes.js | 20 ++++++++++++ src/router/Router/Router.js | 56 ++++++++++++++++++---------------- 3 files changed, 55 insertions(+), 28 deletions(-) create mode 100644 src/App/withProtectedRoutes.js diff --git a/src/App/App.js b/src/App/App.js index 77a4b5824..f0752dc3f 100644 --- a/src/App/App.js +++ b/src/App/App.js @@ -6,13 +6,16 @@ const { useTranslation } = require('react-i18next'); const { Router } = require('stremio-router'); const { Core, Shell, Chromecast, DragAndDrop, KeyboardShortcuts, ServicesProvider } = require('stremio/services'); const { NotFound } = require('stremio/routes'); -const { ToastProvider, CONSTANTS } = require('stremio/common'); +const { ToastProvider, CONSTANTS, withCoreSuspender } = require('stremio/common'); const ServicesToaster = require('./ServicesToaster'); const DeepLinkHandler = require('./DeepLinkHandler'); const ErrorDialog = require('./ErrorDialog'); +const withProtectedRoutes = require('./withProtectedRoutes'); const routerViewsConfig = require('./routerViewsConfig'); const styles = require('./styles'); +const RouterWithProtectedRoutes = withCoreSuspender(withProtectedRoutes(Router)); + const App = () => { const { i18n } = useTranslation(); const onPathNotMatch = React.useCallback(() => { @@ -152,7 +155,7 @@ const App = () => { - { + return function withProtectedRoutes(props) { + const profile = useProfile(); + const onRouteChange = React.useCallback((routeConfig) => { + if (profile.auth !== null && routeConfig.component === Intro) { + window.location.replace('#/'); + return true; + } + }, [profile]); + return ( + + ); + } +}; + +module.exports = withProtectedRoutes; diff --git a/src/router/Router/Router.js b/src/router/Router/Router.js index d442827c8..db4c9e957 100644 --- a/src/router/Router/Router.js +++ b/src/router/Router/Router.js @@ -11,7 +11,7 @@ const Route = require('../Route'); const routeConfigForPath = require('./routeConfigForPath'); const urlParamsForPath = require('./urlParamsForPath'); -const Router = ({ className, onPathNotMatch, ...props }) => { +const Router = ({ className, onPathNotMatch, onRouteChange, ...props }) => { const viewsConfig = React.useMemo(() => props.viewsConfig, []); const [views, setViews] = React.useState(() => { return Array(viewsConfig.length).fill(null); @@ -42,37 +42,40 @@ const Router = ({ className, onPathNotMatch, ...props }) => { const urlParams = urlParamsForPath(routeConfig, typeof pathname === 'string' ? pathname : ''); const routeViewIndex = viewsConfig.findIndex((vc) => vc.includes(routeConfig)); const routeIndex = viewsConfig[routeViewIndex].findIndex((rc) => rc === routeConfig); - setViews((views) => { - return views - .slice(0, viewsConfig.length) - .map((view, index) => { - if (index < routeViewIndex) { - return view; - } else if (index === routeViewIndex) { - return { - key: `${routeViewIndex}${routeIndex}`, - component: routeConfig.component, - urlParams: view !== null && isEqual(view.urlParams, urlParams) ? - view.urlParams - : - urlParams, - queryParams: view !== null && isEqual(Array.from(view.queryParams.entries()), Array.from(queryParams.entries())) ? - view.queryParams - : - queryParams - }; - } else { - return null; - } - }); - }); + const handled = typeof onRouteChange === 'function' && onRouteChange(routeConfig, urlParams, queryParams); + if (!handled) { + setViews((views) => { + return views + .slice(0, viewsConfig.length) + .map((view, index) => { + if (index < routeViewIndex) { + return view; + } else if (index === routeViewIndex) { + return { + key: `${routeViewIndex}${routeIndex}`, + component: routeConfig.component, + urlParams: view !== null && isEqual(view.urlParams, urlParams) ? + view.urlParams + : + urlParams, + queryParams: view !== null && isEqual(Array.from(view.queryParams.entries()), Array.from(queryParams.entries())) ? + view.queryParams + : + queryParams + }; + } else { + return null; + } + }); + }); + } }; window.addEventListener('hashchange', onLocationHashChange); onLocationHashChange(); return () => { window.removeEventListener('hashchange', onLocationHashChange); }; - }, [onPathNotMatch]); + }, [onPathNotMatch, onRouteChange]); return (
{ @@ -93,6 +96,7 @@ const Router = ({ className, onPathNotMatch, ...props }) => { Router.propTypes = { className: PropTypes.string, onPathNotMatch: PropTypes.func, + onRouteChange: PropTypes.func, viewsConfig: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.exact({ regexp: PropTypes.instanceOf(RegExp).isRequired, urlParamsNames: PropTypes.arrayOf(PropTypes.string).isRequired,