Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set correct return url based on path. #16

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 [, 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, , 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