separate "should authenticate" logic from "do authenticate" logic

but don't maintain separate methods for query string vs. props
This commit is contained in:
Michael Bradley, Jr 2018-10-25 05:48:39 -05:00
parent a5c972903b
commit 8a9b8f25ab
1 changed files with 31 additions and 23 deletions

View File

@ -24,38 +24,46 @@ import {
} from '../reducers/selectors';
class AppContainer extends Component {
queryStringAuthenticate() {
if (this.props.credentials.authenticating) {
return;
}
const token = getQueryToken(this.props.lcation);
if (!token) {
return;
}
const host = DEFAULT_HOST;
if (token === this.props.credentials.token && this.props.credentials.host === host) {
return;
}
return this.props.authenticate(host, token);
}
componentDidMount() {
this.props.fetchCredentials();
this.props.fetchTheme();
}
doAuthenticate() {
let {host, token} = this.props.credentials;
const queryToken = getQueryToken(this.props.location);
if (queryToken) {
host = DEFAULT_HOST;
token = queryToken;
}
this.props.authenticate(host, token);
}
requireAuthentication() {
return !(this.props.credentials.authenticating ||
this.props.credentials.authenticated) &&
this.props.credentials.token &&
this.props.credentials.host;
if (this.props.credentials.authenticating) {
return false;
}
const queryToken = getQueryToken(this.props.location);
if (queryToken && !(queryToken === this.props.credentials.token &&
this.props.credentials.host === DEFAULT_HOST)) {
return true;
}
if (!this.props.credentials.authenticated &&
this.props.credentials.host &&
this.props.credentials.token) {
return true;
}
return false;
}
componentDidUpdate(){
if (!this.queryStringAuthenticate() && this.requireAuthentication()) {
this.props.authenticate(this.props.credentials.host, this.props.credentials.token);
if (this.requireAuthentication()) {
this.doAuthenticate();
}
if (getQueryToken(this.props.location) &&