From e582951fe9da1c9062d5edd1d1c12c27983f9910 Mon Sep 17 00:00:00 2001 From: Rob Gietema Date: Mon, 22 Jan 2024 11:50:18 +0100 Subject: [PATCH 1/2] Set correct return url based on path. --- src/components/Login/Login.jsx | 16 ++++++++++++++++ .../LoginAuthomatic/LoginAuthomatic.jsx | 8 ++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/components/Login/Login.jsx b/src/components/Login/Login.jsx index 6869b40..951309f 100644 --- a/src/components/Login/Login.jsx +++ b/src/components/Login/Login.jsx @@ -6,7 +6,20 @@ import React, { useEffect, useState } from 'react'; import { authomaticRedirect, listAuthOptions, oidcRedirect } from '../../actions'; import { injectIntl } from 'react-intl'; import { useSelector, useDispatch } from 'react-redux'; +import { useLocation } from 'react-router-dom'; import LoginForm from './LoginForm'; +import qs from 'query-string'; +import { useCookies } from 'react-cookie'; + +/** + * Get retur url function. + * @function getReturnUrl + * @param {Object} location Location object. + * @returns {string} Return url. + */ +function getReturnUrl(location) { + return `${qs.parse(location.search).return_url || (location.pathname === '/login' ? '/' : location.pathname.replace('/login', ''))}`; +} /** * Login function. @@ -21,6 +34,8 @@ function Login({ intl }) { const options = useSelector((state) => state.authOptions.options); const loginOAuthValues = useSelector((state) => state.authomaticRedirect); const loginOIDCValues = useSelector((state) => state.oidcRedirect); + const location = useLocation(); + const [cookies, setCookie] = useCookies(); useEffect(() => { dispatch(listAuthOptions()); @@ -40,6 +55,7 @@ function Login({ intl }) { const onSelectProvider = (provider) => { setStartedOAuth(true); + setCookie('return_url', getReturnUrl(location), { path: '/' }); dispatch(authomaticRedirect(provider.id)); }; diff --git a/src/components/LoginAuthomatic/LoginAuthomatic.jsx b/src/components/LoginAuthomatic/LoginAuthomatic.jsx index e383c1d..4d88a24 100644 --- a/src/components/LoginAuthomatic/LoginAuthomatic.jsx +++ b/src/components/LoginAuthomatic/LoginAuthomatic.jsx @@ -10,6 +10,7 @@ import { Toast } from '@plone/volto/components'; import { defineMessages, injectIntl } from 'react-intl'; import { useParams, useLocation, useHistory } from 'react-router-dom'; import { useSelector, useDispatch } from 'react-redux'; +import { useCookies } from 'react-cookie'; const messages = defineMessages({ oAuthLoginFailed: { @@ -41,6 +42,8 @@ function LoginAuthomatic({ intl }) { const isLoading = userSession.login.loading; const error = userSession.login.error; const token = userSession.token; + const [cookies, setCookie, removeCookie] = useCookies(); + const return_url = cookies.return_url || '/'; useEffect(() => { dispatch(authomaticLogin(provider, query, session)); @@ -48,12 +51,13 @@ function LoginAuthomatic({ intl }) { useEffect(() => { if (token) { - history.push('/'); + history.push(return_url); + window.setTimeout(() => removeCookie('return_url', { path: '/' }), 500); if (toast.isActive('loginFailed')) { toast.dismiss('loginFailed'); } } - }, [token, history]); + }, [token, history, removeCookie, return_url]); useEffect(() => { if (error) { From b2bde7a35230a2140bb37b7bd490380f74f758a1 Mon Sep 17 00:00:00 2001 From: Rob Gietema Date: Mon, 22 Jan 2024 11:59:55 +0100 Subject: [PATCH 2/2] Fix linting. --- src/components/Login/Login.jsx | 2 +- src/components/LoginAuthomatic/LoginAuthomatic.jsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/Login/Login.jsx b/src/components/Login/Login.jsx index 951309f..55e0070 100644 --- a/src/components/Login/Login.jsx +++ b/src/components/Login/Login.jsx @@ -35,7 +35,7 @@ function Login({ intl }) { const loginOAuthValues = useSelector((state) => state.authomaticRedirect); const loginOIDCValues = useSelector((state) => state.oidcRedirect); const location = useLocation(); - const [cookies, setCookie] = useCookies(); + const [, setCookie] = useCookies(); useEffect(() => { dispatch(listAuthOptions()); diff --git a/src/components/LoginAuthomatic/LoginAuthomatic.jsx b/src/components/LoginAuthomatic/LoginAuthomatic.jsx index 4d88a24..445ebc4 100644 --- a/src/components/LoginAuthomatic/LoginAuthomatic.jsx +++ b/src/components/LoginAuthomatic/LoginAuthomatic.jsx @@ -42,7 +42,7 @@ function LoginAuthomatic({ intl }) { const isLoading = userSession.login.loading; const error = userSession.login.error; const token = userSession.token; - const [cookies, setCookie, removeCookie] = useCookies(); + const [cookies, , removeCookie] = useCookies(); const return_url = cookies.return_url || '/'; useEffect(() => {