1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

Merge pull request #730 from Unleash/feat/redirect

feat: add redirect params to /login
This commit is contained in:
Youssef Khedher 2022-03-09 12:38:57 +01:00 committed by GitHub
commit cd59d7d84b
8 changed files with 28 additions and 18 deletions

View File

@ -41,7 +41,6 @@ export const App = () => {
const renderRoute = (route: any) => { const renderRoute = (route: any) => {
if (route.type === 'protected') { if (route.type === 'protected') {
const unauthorized = isUnauthorized(); const unauthorized = isUnauthorized();
return ( return (
<ProtectedRoute <ProtectedRoute
key={route.path} key={route.path}

View File

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

View File

@ -15,7 +15,10 @@ import ConditionallyRender from '../../common/ConditionallyRender';
import { Alert } from '@material-ui/lab'; import { Alert } from '@material-ui/lab';
import { useAuthDetails } from '../../../hooks/api/getters/useAuth/useAuthDetails'; import { useAuthDetails } from '../../../hooks/api/getters/useAuth/useAuthDetails';
const Authentication = () => { interface IAuthenticationProps {
redirect: string;
}
const Authentication = ({ redirect }: IAuthenticationProps) => {
const { authDetails } = useAuthDetails(); const { authDetails } = useAuthDetails();
const params = useQueryParams(); const params = useQueryParams();
@ -26,7 +29,7 @@ const Authentication = () => {
if (authDetails.type === PASSWORD_TYPE) { if (authDetails.type === PASSWORD_TYPE) {
content = ( content = (
<> <>
<PasswordAuth authDetails={authDetails} /> <PasswordAuth authDetails={authDetails} redirect={redirect} />
<ConditionallyRender <ConditionallyRender
condition={!authDetails.defaultHidden} condition={!authDetails.defaultHidden}
show={<SecondaryLoginActions />} show={<SecondaryLoginActions />}
@ -34,13 +37,13 @@ const Authentication = () => {
</> </>
); );
} else if (authDetails.type === SIMPLE_TYPE) { } else if (authDetails.type === SIMPLE_TYPE) {
content = <SimpleAuth authDetails={authDetails} />; content = <SimpleAuth authDetails={authDetails} redirect={redirect} />;
} else if (authDetails.type === DEMO_TYPE) { } else if (authDetails.type === DEMO_TYPE) {
content = <DemoAuth authDetails={authDetails} />; content = <DemoAuth authDetails={authDetails} redirect={redirect} />;
} else if (authDetails.type === HOSTED_TYPE) { } else if (authDetails.type === HOSTED_TYPE) {
content = ( content = (
<> <>
<HostedAuth authDetails={authDetails} /> <HostedAuth authDetails={authDetails} redirect={redirect} />
<ConditionallyRender <ConditionallyRender
condition={!authDetails.defaultHidden} condition={!authDetails.defaultHidden}
show={<SecondaryLoginActions />} show={<SecondaryLoginActions />}

View File

@ -10,7 +10,7 @@ import { useAuthUser } from '../../../hooks/api/getters/useAuth/useAuthUser';
import useToast from '../../../hooks/useToast'; import useToast from '../../../hooks/useToast';
import { formatUnknownError } from '../../../utils/format-unknown-error'; import { formatUnknownError } from '../../../utils/format-unknown-error';
const DemoAuth = ({ authDetails }) => { const DemoAuth = ({ authDetails, redirect }) => {
const [email, setEmail] = useState(''); const [email, setEmail] = useState('');
const history = useHistory(); const history = useHistory();
const { refetchUser } = useAuthUser(); const { refetchUser } = useAuthUser();
@ -23,7 +23,7 @@ const DemoAuth = ({ authDetails }) => {
try { try {
await emailAuth(authDetails.path, email); await emailAuth(authDetails.path, email);
refetchUser(); refetchUser();
history.push(`/`); history.push(redirect);
} catch (error) { } catch (error) {
setToastApiError(formatUnknownError(error)); setToastApiError(formatUnknownError(error));
} }
@ -91,6 +91,7 @@ const DemoAuth = ({ authDetails }) => {
DemoAuth.propTypes = { DemoAuth.propTypes = {
authDetails: PropTypes.object.isRequired, authDetails: PropTypes.object.isRequired,
redirect: PropTypes.string.isRequired,
}; };
export default DemoAuth; export default DemoAuth;

View File

@ -18,7 +18,7 @@ import {
LOGIN_PASSWORD_ID, LOGIN_PASSWORD_ID,
} from '../../../testIds'; } from '../../../testIds';
const HostedAuth = ({ authDetails }) => { const HostedAuth = ({ authDetails, redirect }) => {
const commonStyles = useCommonStyles(); const commonStyles = useCommonStyles();
const styles = useStyles(); const styles = useStyles();
const { refetchUser } = useAuthUser(); const { refetchUser } = useAuthUser();
@ -55,7 +55,7 @@ const HostedAuth = ({ authDetails }) => {
try { try {
await passwordAuth(authDetails.path, username, password); await passwordAuth(authDetails.path, username, password);
refetchUser(); refetchUser();
history.push(`/`); history.push(redirect);
} catch (error) { } catch (error) {
if (error.statusCode === 404 || error.statusCode === 400) { if (error.statusCode === 404 || error.statusCode === 400) {
setErrors(prev => ({ setErrors(prev => ({
@ -145,6 +145,7 @@ const HostedAuth = ({ authDetails }) => {
HostedAuth.propTypes = { HostedAuth.propTypes = {
authDetails: PropTypes.object.isRequired, authDetails: PropTypes.object.isRequired,
redirect: PropTypes.string.isRequired,
}; };
export default HostedAuth; export default HostedAuth;

View File

@ -15,9 +15,10 @@ const Login = () => {
const { user } = useAuthUser(); const { user } = useAuthUser();
const query = useQueryParams(); const query = useQueryParams();
const resetPassword = query.get('reset') === 'true'; const resetPassword = query.get('reset') === 'true';
const redirect = query.get('redirect') || '/';
if (user) { if (user) {
return <Redirect to="/features" />; return <Redirect to={redirect} />;
} }
return ( return (
@ -36,7 +37,7 @@ const Login = () => {
condition={resetPassword} condition={resetPassword}
show={<ResetPasswordSuccess />} show={<ResetPasswordSuccess />}
/> />
<Authentication /> <Authentication redirect={redirect} />
</div> </div>
</StandaloneLayout> </StandaloneLayout>
); );

View File

@ -19,7 +19,7 @@ import PasswordField from '../../common/PasswordField/PasswordField';
import { useAuthApi } from '../../../hooks/api/actions/useAuthApi/useAuthApi'; import { useAuthApi } from '../../../hooks/api/actions/useAuthApi/useAuthApi';
import { useAuthUser } from '../../../hooks/api/getters/useAuth/useAuthUser'; import { useAuthUser } from '../../../hooks/api/getters/useAuth/useAuthUser';
const PasswordAuth = ({ authDetails }) => { const PasswordAuth = ({ authDetails, redirect }) => {
const commonStyles = useCommonStyles(); const commonStyles = useCommonStyles();
const styles = useStyles(); const styles = useStyles();
const history = useHistory(); const history = useHistory();
@ -56,7 +56,7 @@ const PasswordAuth = ({ authDetails }) => {
try { try {
await passwordAuth(authDetails.path, username, password); await passwordAuth(authDetails.path, username, password);
refetchUser(); refetchUser();
history.push(`/`); history.push(redirect);
} catch (error) { } catch (error) {
if (error.statusCode === 404 || error.statusCode === 400) { if (error.statusCode === 404 || error.statusCode === 400) {
setErrors(prev => ({ setErrors(prev => ({
@ -168,6 +168,7 @@ const PasswordAuth = ({ authDetails }) => {
PasswordAuth.propTypes = { PasswordAuth.propTypes = {
authDetails: PropTypes.object.isRequired, authDetails: PropTypes.object.isRequired,
redirect: PropTypes.string.isRequired,
}; };
export default PasswordAuth; export default PasswordAuth;

View File

@ -9,7 +9,7 @@ import { LOGIN_BUTTON, LOGIN_EMAIL_ID } from '../../../testIds';
import useToast from '../../../hooks/useToast'; import useToast from '../../../hooks/useToast';
import { formatUnknownError } from '../../../utils/format-unknown-error'; import { formatUnknownError } from '../../../utils/format-unknown-error';
const SimpleAuth = ({ authDetails }) => { const SimpleAuth = ({ authDetails, redirect }) => {
const [email, setEmail] = useState(''); const [email, setEmail] = useState('');
const { refetchUser } = useAuthUser(); const { refetchUser } = useAuthUser();
const { emailAuth } = useAuthApi(); const { emailAuth } = useAuthApi();
@ -22,7 +22,7 @@ const SimpleAuth = ({ authDetails }) => {
try { try {
await emailAuth(authDetails.path, email); await emailAuth(authDetails.path, email);
refetchUser(); refetchUser();
history.push(`/`); history.push(redirect);
} catch (error) { } catch (error) {
setToastApiError(formatUnknownError(error)); setToastApiError(formatUnknownError(error));
} }
@ -80,6 +80,7 @@ const SimpleAuth = ({ authDetails }) => {
SimpleAuth.propTypes = { SimpleAuth.propTypes = {
authDetails: PropTypes.object.isRequired, authDetails: PropTypes.object.isRequired,
redirect: PropTypes.string.isRequired,
}; };
export default SimpleAuth; export default SimpleAuth;