Skip to content

Commit

Permalink
Set correct return url based on path.
Browse files Browse the repository at this point in the history
  • Loading branch information
robgietema committed Jan 22, 2024
1 parent 6de0dda commit e582951
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/components/Login/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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());
Expand All @@ -40,6 +55,7 @@ function Login({ intl }) {

const onSelectProvider = (provider) => {
setStartedOAuth(true);
setCookie('return_url', getReturnUrl(location), { path: '/' });
dispatch(authomaticRedirect(provider.id));
};

Expand Down
8 changes: 6 additions & 2 deletions src/components/LoginAuthomatic/LoginAuthomatic.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -41,19 +42,22 @@ 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));
}, [dispatch, provider, query, session]);

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) {
Expand Down

0 comments on commit e582951

Please sign in to comment.