From b762cd6b21ebb70231c6fa1b9732a1940b17bb58 Mon Sep 17 00:00:00 2001 From: Piyush Date: Thu, 6 Jun 2024 11:33:58 +0530 Subject: [PATCH] Fix redundant redirects between client and server --- .../components/createRedirectWithUsername.jsx | 29 ------------------ client/routes.jsx | 30 ++++--------------- client/utils/auth.js | 30 ------------------- server/routes/server.routes.js | 14 ++++++--- 4 files changed, 15 insertions(+), 88 deletions(-) delete mode 100644 client/components/createRedirectWithUsername.jsx delete mode 100644 client/utils/auth.js diff --git a/client/components/createRedirectWithUsername.jsx b/client/components/createRedirectWithUsername.jsx deleted file mode 100644 index a6b69233c4..0000000000 --- a/client/components/createRedirectWithUsername.jsx +++ /dev/null @@ -1,29 +0,0 @@ -import React from 'react'; -import { connect } from 'react-redux'; -import browserHistory from '../browserHistory'; - -const RedirectToUser = ({ username, url = '/:username/sketches' }) => { - React.useEffect(() => { - if (username == null) { - return; - } - - browserHistory.replace(url.replace(':username', username)); - }, [username]); - - return null; -}; - -function mapStateToProps(state) { - return { - username: state.user ? state.user.username : null - }; -} - -const ConnectedRedirectToUser = connect(mapStateToProps)(RedirectToUser); - -const createRedirectWithUsername = (url) => (props) => ( - -); - -export default createRedirectWithUsername; diff --git a/client/routes.jsx b/client/routes.jsx index 418a5bf99d..98eadca485 100644 --- a/client/routes.jsx +++ b/client/routes.jsx @@ -17,13 +17,7 @@ import NewPasswordView from './modules/User/pages/NewPasswordView'; import AccountView from './modules/User/pages/AccountView'; import CollectionView from './modules/User/pages/CollectionView'; import DashboardView from './modules/User/pages/DashboardView'; -import createRedirectWithUsername from './components/createRedirectWithUsername'; import { getUser } from './modules/User/actions'; -import { - userIsAuthenticated, - userIsNotAuthenticated, - userIsAuthorized -} from './utils/auth'; /** * `params` is no longer a top-level route component prop in v4. @@ -51,25 +45,19 @@ Route.propTypes = { const routes = ( - - + + - + - + - - - + diff --git a/client/utils/auth.js b/client/utils/auth.js deleted file mode 100644 index 6f2fb4e21d..0000000000 --- a/client/utils/auth.js +++ /dev/null @@ -1,30 +0,0 @@ -import { connectedRouterRedirect } from 'redux-auth-wrapper/history4/redirect'; -import locationHelperBuilder from 'redux-auth-wrapper/history4/locationHelper'; - -const locationHelper = locationHelperBuilder({}); - -export const userIsAuthenticated = connectedRouterRedirect({ - // The url to redirect user to if they fail - redirectPath: '/login', - // Determine if the user is authenticated or not - authenticatedSelector: (state) => state.user.authenticated === true, - // A nice display name for this check - wrapperDisplayName: 'UserIsAuthenticated' -}); - -export const userIsNotAuthenticated = connectedRouterRedirect({ - redirectPath: (state, ownProps) => - locationHelper.getRedirectQueryParam(ownProps) || '/', - allowRedirectBack: false, - authenticatedSelector: (state) => state.user.authenticated === false, - wrapperDisplayName: 'UserIsNotAuthenticated' -}); - -export const userIsAuthorized = connectedRouterRedirect({ - redirectPath: '/', - allowRedirectBack: false, - authenticatedSelector: (state, ownProps) => { - const { username } = ownProps.params; - return state.user.username === username; - } -}); diff --git a/server/routes/server.routes.js b/server/routes/server.routes.js index eed2ea87da..0c2f921739 100644 --- a/server/routes/server.routes.js +++ b/server/routes/server.routes.js @@ -76,11 +76,17 @@ router.get('/login', (req, res) => { }); router.get('/reset-password', (req, res) => { - res.send(renderIndex()); + if (req.user) { + return res.redirect('/account'); + } + return res.send(renderIndex()); }); router.get('/reset-password/:reset_password_token', (req, res) => { - res.send(renderIndex()); + if (req.user) { + return res.redirect('/account'); + } + return res.send(renderIndex()); }); router.get('/verify', (req, res) => { @@ -89,7 +95,7 @@ router.get('/verify', (req, res) => { router.get('/sketches', (req, res) => { if (req.user) { - res.send(renderIndex()); + res.redirect(`/${req.user.username}/sketches`); } else { res.redirect('/login'); } @@ -97,7 +103,7 @@ router.get('/sketches', (req, res) => { router.get('/assets', (req, res) => { if (req.user) { - res.send(renderIndex()); + res.redirect(`/${req.user.username}/assets`); } else { res.redirect('/login'); }