Skip to content

Commit e28cc8a

Browse files
committed
fix login redirect
1 parent be73b73 commit e28cc8a

4 files changed

Lines changed: 529 additions & 401 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"passport-facebook-token": "^3.3.0",
9393
"pretty-error": "^2.0.2",
9494
"prop-types": "^15.5.8",
95+
"qs": "^6.5.2",
9596
"react": "^16.3.1",
9697
"react-bootstrap": "^0.32.1",
9798
"react-dom": "^16.3.1",

src/containers/App/App.js

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _ from 'lodash';
12
import React, { Component } from 'react';
23
import PropTypes from 'prop-types';
34
import { connect } from 'react-redux';
@@ -11,6 +12,7 @@ import Nav from 'react-bootstrap/lib/Nav';
1112
import NavItem from 'react-bootstrap/lib/NavItem';
1213
import Alert from 'react-bootstrap/lib/Alert';
1314
import Helmet from 'react-helmet';
15+
import qs from 'qs';
1416
import { isLoaded as isInfoLoaded, load as loadInfo } from 'redux/modules/info';
1517
import { isLoaded as isAuthLoaded, load as loadAuth, logout } from 'redux/modules/auth';
1618
import { Notifs, InfoBar } from 'components';
@@ -56,39 +58,30 @@ export default class App extends Component {
5658
store: PropTypes.object.isRequired
5759
};
5860

59-
static getDerivedStateFromProps(nextProps, prevState) {
60-
if (!prevState.user && nextProps.user) {
61-
const query = new URLSearchParams(nextProps.location.search);
62-
nextProps.pushState(query.get('redirect') || '/login-success');
63-
return {
64-
user: nextProps.user
65-
};
66-
} else if (prevState.user && !nextProps.user) {
67-
nextProps.pushState('/');
68-
return {
69-
user: null
70-
};
61+
static getDerivedStateFromProps(props, state) {
62+
const { prevProps } = state;
63+
// Compare the incoming prop to previous prop
64+
const user = !_.isEqual(prevProps.user, props.user) ? props.user : state.user;
65+
66+
if (!prevProps.user && props.user) {
67+
const query = qs.parse(props.location.search, { ignoreQueryPrefix: true });
68+
props.pushState(query.redirect || '/login-success');
69+
} else if (prevProps.user && !props.user) {
70+
// logout
71+
props.pushState('/');
7172
}
72-
return null;
73+
74+
return {
75+
// Store the previous props in state
76+
prevProps: props,
77+
user
78+
};
7379
}
7480

7581
state = {
76-
user: null
82+
prevProps: this.props, // eslint-disable-line react/no-unused-state
83+
user: this.props.user
7784
};
78-
/* Made Unsafe as per React Docs v16.3 */
79-
// componentWillReceiveProps(nextProps) {
80-
// console.log(this.props.user);
81-
// console.log(nextProps.user);
82-
// if (!this.props.user && nextProps.user) {
83-
// // login
84-
// const query = new URLSearchParams(this.props.location.search);
85-
// this.props.pushState(query.get('redirect') || '/login-success');
86-
// } else if (this.props.user && !nextProps.user) {
87-
// // logout
88-
// this.props.pushState('/');
89-
// }
90-
// }
91-
/* Unsafe as per v16.3 */
9285

9386
componentDidUpdate(prevProps) {
9487
if (this.props.location !== prevProps.location) {

src/containers/Login/Login.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default class Login extends Component {
4343
}
4444
};
4545

46-
login = async data => {
46+
onLocalLogin = async data => {
4747
const result = await this.props.login('local', data);
4848
this.successLogin();
4949
return result;
@@ -71,7 +71,7 @@ export default class Login extends Component {
7171
<h1>Login</h1>
7272
{!user && (
7373
<div>
74-
<LoginForm onSubmit={this.login} />
74+
<LoginForm onSubmit={this.onLocalLogin} />
7575
<p>This will "log you in" as this user, storing the username in the session of the API server.</p>
7676
<FacebookLogin
7777
appId="635147529978862"

0 commit comments

Comments
 (0)