diff --git a/src/components/Login/Login.jsx b/src/components/Login/Login.jsx index 6869b40..55e0070 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 [, 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..445ebc4 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, , 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) {