Skip to content

Commit

Permalink
Protected Pages Show Blank Screen Instead of Redirecting to Login
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Raghuwanshi committed Feb 10, 2025
1 parent bcc1995 commit a5736b9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/volto/src/helpers/Api/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ class Api {
url: err.response.headers.location,
});
}

if (response.status === 401) {
window.location.href = '/login';
}
return err ? reject(err) : resolve(response.body || response.text);
});
});
Expand Down
24 changes: 24 additions & 0 deletions packages/volto/src/protected-routes.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useSelector } from 'react-redux';
import { Route, Redirect } from 'react-router-dom';

export const ProtectedRoutes = ({ component: Component, ...rest }) => {
const token = useSelector((state) => state.userSession.token);

return (
<Route
{...rest}
render={(props) =>
token ? (
<Component {...props} />
) : (
<Redirect
to={{
pathname: '/login',
state: { from: props.location },
}}
/>
)
}
/>
);
};
6 changes: 5 additions & 1 deletion packages/volto/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import App from '@plone/volto/components/theme/App/App';
import View from '@plone/volto/components/theme/View/View';

import config from '@plone/volto/registry';
import { ProtectedRoutes } from './protected-routes';

/**
* Default routes array.
Expand Down Expand Up @@ -232,7 +233,10 @@ export const defaultRoutes = [
},
{
path: '/controlpanel/:id',
component: Controlpanel,
component: (props) => (
// eslint-disable-next-line
<ProtectedRoutes component={Controlpanel} {...props} />
),
},
{
path: '/change-password',
Expand Down

0 comments on commit a5736b9

Please sign in to comment.