Skip to content

Commit

Permalink
Merge pull request #1381 from bjoernricks/store-is-logged-in
Browse files Browse the repository at this point in the history
Fix redirect to login page
  • Loading branch information
bjoernricks authored May 9, 2019
2 parents e21b25e + 82cc0ee commit f119368
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
41 changes: 34 additions & 7 deletions gsa/src/web/authorized.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {withRouter} from 'react-router-dom';

import {isDefined} from 'gmp/utils/identity';

import {setIsLoggedIn} from './store/usersettings/actions';
import {isLoggedIn} from 'web/store/usersettings/selectors';

import compose from 'web/utils/compose';
Expand All @@ -43,6 +44,8 @@ class Authorized extends React.Component {
this.responseError = this.responseError.bind(this);

this.unsubscribe = gmp.addHttpErrorHandler(this.responseError);

this.checkIsLoggedIn();
}

componentWillUnmount() {
Expand All @@ -51,24 +54,37 @@ class Authorized extends React.Component {
}
}

componentDidUpdate() {
this.checkIsLoggedIn();
}

responseError(xhr) {
const {location} = this.props;
const {logout} = this.props;

if (xhr.status === 401 && location.pathname !== '/login') {
this.toLoginPage();
if (xhr.status === 401) {
logout();
return Promise.resolve(xhr);
}
return Promise.reject(xhr);
}

checkIsLoggedIn() {
if (!this.props.isLoggedIn) {
this.toLoginPage();
}
}

toLoginPage() {
const {gmp, history} = this.props;
const {history, location} = this.props;

if (location.pathname === '/login') {
// already at login page
return;
}

history.replace('/login', {
next: this.props.location.pathname,
});

gmp.logout();
}

render() {
Expand All @@ -81,16 +97,27 @@ Authorized.propTypes = {
history: PropTypes.object.isRequired,
isLoggedIn: PropTypes.bool.isRequired,
location: PropTypes.object.isRequired,
logout: PropTypes.func.isRequired,
};

const mapStateToProps = rootState => ({
isLoggedIn: isLoggedIn(rootState),
});

const mapDispatchToProps = (dispatch, {gmp}) => ({
logout: () => {
gmp.logout();
dispatch(setIsLoggedIn(false));
},
});

export default compose(
withGmp,
withRouter,
connect(mapStateToProps),
connect(
mapStateToProps,
mapDispatchToProps,
),
)(Authorized);

// vim: set ts=2 sw=2 tw=80:
14 changes: 8 additions & 6 deletions gsa/src/web/pages/login/loginpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ class LoginPage extends React.Component {
const {locale, timezone, sessionTimeout} = data;

const {location, history} = this.props;

this.props.setTimezone(timezone);
this.props.setLocale(locale);
this.props.setSessionTimeout(sessionTimeout);
this.props.setUsername(username);
// must be set before changing the location
this.props.setIsLoggedIn(true);

if (
location &&
location.state &&
Expand All @@ -143,12 +151,6 @@ class LoginPage extends React.Component {
} else {
history.replace('/');
}

this.props.setTimezone(timezone);
this.props.setLocale(locale);
this.props.setSessionTimeout(sessionTimeout);
this.props.setUsername(username);
this.props.setIsLoggedIn(true);
},
rej => {
log.error(rej);
Expand Down

0 comments on commit f119368

Please sign in to comment.