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

Logout Refactor #4860

Merged
merged 51 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
df9b3ba
refactor logout
Tishasoumya-02 Jun 7, 2023
743b93d
refactor logout
Tishasoumya-02 Jun 7, 2023
addddaa
Logout cypress test & added useCallback hook
Tishasoumya-02 Jun 9, 2023
454346f
Removed cypress test errors
Tishasoumya-02 Jun 9, 2023
0d3e52a
Merge branch 'master' of https://github.com/plone/volto into logout-r…
Tishasoumya-02 Jun 9, 2023
262a31c
UseMemo() added
Tishasoumya-02 Jun 9, 2023
8dec141
Prettier formated
Tishasoumya-02 Jun 9, 2023
df79cf9
/new
Tishasoumya-02 Jun 9, 2023
7a53530
Removed proptypes error ,added required func props in test file
Tishasoumya-02 Jun 15, 2023
2609965
Prettier
Tishasoumya-02 Jun 15, 2023
61e7477
Merge branch 'master' of https://github.com/plone/volto into logout-r…
Tishasoumya-02 Jun 19, 2023
e78abb9
Add documentation to hooks, restore test ,remove unecessary proptypes
Tishasoumya-02 Jun 19, 2023
acb9f32
Assert auth_key is null
Tishasoumya-02 Jun 19, 2023
f454c46
Assert auth_key is null
Tishasoumya-02 Jun 19, 2023
845f12b
Assert auth_key is null
Tishasoumya-02 Jun 19, 2023
0954a40
Cypress test
Tishasoumya-02 Jun 19, 2023
b2c3876
dependency array
Tishasoumya-02 Jun 19, 2023
0a7218c
Remove unecessary proptypes
Tishasoumya-02 Jun 20, 2023
24f1904
Remove error
Tishasoumya-02 Jun 20, 2023
bc2a778
Remove error
Tishasoumya-02 Jun 20, 2023
6d2b588
Merge branch 'master' into logout-refactor
nileshgulia1 Jun 24, 2023
d5e073b
Merge branch 'master' of https://github.com/plone/volto into logout-r…
Tishasoumya-02 Jun 26, 2023
2772217
Merge branch 'logout-refactor' of https://github.com/plone/volto into…
Tishasoumya-02 Jun 26, 2023
c40ad8d
hooks/index.js
Tishasoumya-02 Jun 26, 2023
905d679
hooks/index
Tishasoumya-02 Jun 26, 2023
8c507e8
hooks/index
Tishasoumya-02 Jun 26, 2023
ab9baf2
hooks/index
Tishasoumya-02 Jun 26, 2023
abca8b9
hooks/index
Tishasoumya-02 Jun 26, 2023
2524194
Revert
Tishasoumya-02 Jun 26, 2023
9789fc1
Prettier
Tishasoumya-02 Jun 26, 2023
136cf44
hooks/index
Tishasoumya-02 Jun 26, 2023
f3000ac
hooks/index
Tishasoumya-02 Jun 26, 2023
510736c
Update Logout.jsx
Tishasoumya-02 Jun 26, 2023
4ad931e
Merge branch 'master' into logout-refactor
Tishasoumya-02 Jun 26, 2023
3116063
Merge branch 'master' into logout-refactor
Tishasoumya-02 Jun 29, 2023
7db0f2d
Merge branch 'master' of https://github.com/plone/volto into logout-r…
Tishasoumya-02 Jul 5, 2023
8b0014d
Merge branch 'logout-refactor' of https://github.com/plone/volto into…
Tishasoumya-02 Jul 5, 2023
8c223be
Move useSelector() in the component
Tishasoumya-02 Jul 5, 2023
6dd15b9
use redux hooks directly
Tishasoumya-02 Jul 8, 2023
a4b9262
Merge branch 'master' into logout-refactor
Tishasoumya-02 Jul 8, 2023
fd63b91
remove usetoken hook
Tishasoumya-02 Jul 11, 2023
055c0ce
Merge branch 'logout-refactor' of https://github.com/plone/volto into…
Tishasoumya-02 Jul 11, 2023
3d7d1ef
useLocation hook
Tishasoumya-02 Jul 11, 2023
8d7d607
remove useLocation hook
Tishasoumya-02 Jul 11, 2023
19e8854
Merge branch 'master' into logout-refactor
Tishasoumya-02 Jul 12, 2023
5157b6d
Merge branch 'master' of https://github.com/plone/volto into logout-r…
Tishasoumya-02 Aug 9, 2023
d508aa1
Merge branch 'master' into logout-refactor
nileshgulia1 Aug 14, 2023
651e759
Merge branch 'master' of https://github.com/plone/volto into logout-r…
Tishasoumya-02 Aug 14, 2023
8bf186b
delete userSession
Tishasoumya-02 Aug 14, 2023
b79dc41
Merge branch 'logout-refactor' of https://github.com/plone/volto into…
Tishasoumya-02 Aug 14, 2023
194f195
Merge branch 'master' into logout-refactor
nileshgulia1 Aug 22, 2023
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
20 changes: 20 additions & 0 deletions cypress/tests/core/basic/logout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ploneAuth } from '../../../support/constants';

describe('Logout Tests', () => {
beforeEach(() => {
cy.visit('/');
cy.contains('Log in').click();
const user = ploneAuth[0];
const password = ploneAuth[1];

cy.get('#login').type(user).should('have.value', user);
cy.get('#password').type(password).should('have.value', password);
cy.get('#login-form-submit').click();
cy.get('body').should('have.class', 'has-toolbar');
});
it('As registered user I can logout', function () {
cy.get('#toolbar-personal').click();
cy.get('#toolbar-logout').click();
nileshgulia1 marked this conversation as resolved.
Show resolved Hide resolved
cy.getCookie('auth_key').should('not.exist');
});
});
1 change: 1 addition & 0 deletions news/4860.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactor Logout component @Tishasoumya-02
119 changes: 36 additions & 83 deletions src/components/theme/Logout/Logout.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
/**
* Login container.
* @module components/theme/Logout/Logout
*/

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { compose } from 'redux';
import { defineMessages, injectIntl } from 'react-intl';
import { useEffect, useMemo } from 'react';
import { useDispatch, useSelector, shallowEqual } from 'react-redux';
import { useHistory } from 'react-router-dom';
import { defineMessages, useIntl } from 'react-intl';
import qs from 'query-string';

import { Login } from '@plone/volto/components';
import { Login, Toast } from '@plone/volto/components';
import { logout, purgeMessages } from '@plone/volto/actions';
import { toast } from 'react-toastify';
import { Toast } from '@plone/volto/components';

const messages = defineMessages({
loggedOut: {
Expand All @@ -26,83 +18,44 @@ const messages = defineMessages({
},
});

/**
* Logout class.
* @class Logout
* @extends Component
*/
class Logout extends Component {
/**
* Property types.
* @property {Object} propTypes Property types.
* @static
*/
static propTypes = {
logout: PropTypes.func.isRequired,
purgeMessages: PropTypes.func.isRequired,
query: PropTypes.shape({
return_url: PropTypes.string,
}),
};

/**
* Default properties.
* @property {Object} defaultProps Default properties.
* @static
*/
static defaultProps = {
query: null,
};

componentDidMount() {
this.props.logout();
this.props.purgeMessages();
}

/**
* Component will receive props
* @method componentWillReceiveProps
* @param {Object} nextProps Next properties
* @returns {undefined}
*/
UNSAFE_componentWillReceiveProps(nextProps) {
if (!nextProps.token) {
this.props.history.replace(this.props.returnUrl || '/');
const Logout = ({ location }) => {
const token = useSelector((state) => state.userSession.token, shallowEqual);
const history = useHistory();
const dispatch = useDispatch();
const intl = useIntl();

const returnUrl = useMemo(
() =>
qs.parse(location.search).return_url ||
location.pathname
.replace(/\/login\/?$/, '')
.replace(/\/logout\/?$/, '') ||
'/',
[location],
);

useEffect(() => {
dispatch(logout());
dispatch(purgeMessages());
}, [dispatch]);

useEffect(() => {
if (!token) {
history.replace(returnUrl || '/');
if (!toast.isActive('loggedOut')) {
toast.info(
<Toast
info
title={this.props.intl.formatMessage(messages.loggedOut)}
content={this.props.intl.formatMessage(messages.loggedOutContent)}
title={intl.formatMessage(messages.loggedOut)}
content={intl.formatMessage(messages.loggedOutContent)}
/>,
{ autoClose: false, toastId: 'loggedOut' },
);
}
}
}
}, [history, returnUrl, intl, token]);

return <Login location={{ query: location.query }} />;
};

/**
* Render method.
* @method render
* @returns {string} Markup for the component.
*/
render() {
return <Login location={{ query: this.props.location.query }} />;
}
}
export default compose(
injectIntl,
connect(
(state, props) => ({
query: qs.parse(props.location.search),
token: state.userSession.token,
returnUrl:
qs.parse(props.location.search).return_url ||
props.location.pathname
.replace(/\/login\/?$/, '')
.replace(/\/logout\/?$/, '') ||
'/',
}),
{ logout, purgeMessages },
),
)(Logout);
export default Logout;
14 changes: 14 additions & 0 deletions src/hooks/userSession/useToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useSelector, shallowEqual } from 'react-redux';

/**
* useToken hook
*
* This hook returns the current token that is stored in the Redux store in the
* `userSession` reducer, and returns it along with the related state (token).
*
* @export
* @return {{ token }}
*/
export function useToken() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tishasoumya-02 Where is this useToken used?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you are accessing directly from redux state in your component. We can remove this!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes sure, I might have missed it

return useSelector((state) => state.userSession.token, shallowEqual);
}