1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-04 00:18:01 +01:00

feat: add redirect params to /login

This commit is contained in:
Youssef 2022-02-21 12:33:11 +01:00
parent f466a78193
commit bcbfd3e38c
3 changed files with 12 additions and 6 deletions

View File

@ -41,7 +41,6 @@ export const App = () => {
const renderRoute = (route: any) => {
if (route.type === 'protected') {
const unauthorized = isUnauthorized();
return (
<ProtectedRoute
key={route.path}
@ -88,7 +87,6 @@ export const App = () => {
path="/"
unauthorized={isUnauthorized()}
component={Redirect}
renderProps={{ to: '/features' }}
/>
{renderMainLayoutRoutes()}
{renderStandaloneRoutes()}

View File

@ -1,4 +1,4 @@
import { Route, Redirect } from 'react-router-dom';
import { Route, useLocation, useHistory } from 'react-router-dom';
const ProtectedRoute = ({
component: Component,
@ -6,12 +6,16 @@ const ProtectedRoute = ({
renderProps = {},
...rest
}) => {
const { pathname } = useLocation();
const history = useHistory();
const loginLink =
pathname.length > 1 ? `/login?redirect=${pathname}` : '/login';
return (
<Route
{...rest}
render={props => {
if (unauthorized) {
return <Redirect to={'/login'} />;
history.push(loginLink);
} else {
return <Component {...props} {...renderProps} />;
}

View File

@ -7,7 +7,7 @@ import { DEMO_TYPE } from '../../../constants/authTypes';
import Authentication from '../Authentication/Authentication';
import { useAuthDetails } from '../../../hooks/api/getters/useAuth/useAuthDetails';
import { useAuthUser } from '../../../hooks/api/getters/useAuth/useAuthUser';
import { Redirect } from 'react-router-dom';
import { useHistory } from 'react-router-dom';
const Login = () => {
const styles = useStyles();
@ -15,9 +15,13 @@ const Login = () => {
const { user } = useAuthUser();
const query = useQueryParams();
const resetPassword = query.get('reset') === 'true';
const redirect = query.get('redirect') || '/features';
const history = useHistory();
console.log(redirect);
if (user) {
return <Redirect to="/features" />;
console.log(redirect);
history.push(redirect);
}
return (