Skip to content

Commit

Permalink
Merge pull request aws-amplify#1452 from powerful23/for-issue-1012
Browse files Browse the repository at this point in the history
fix(aws-amplify-react-native): fix the Authenticator to only call async functions when mounted
  • Loading branch information
powerful23 authored Aug 16, 2018
2 parents 39ddb59 + 9ef0b58 commit b218a72
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/aws-amplify-react-native/src/Auth/Authenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,23 @@ export default class Authenticator extends React.Component {
Hub.listen('auth', this);
}

componentWillMount() {
componentDidMount() {
this._isMounted = true;
this.checkUser();
}

componentWillUnmount() {
this._isMounted = false;
}

onHubCapsule(capsule) {
const { channel, payload, source } = capsule;
if (channel === 'auth') { this.checkUser(); }
}

handleStateChange(state, data) {
logger.debug('authenticator state change ' + state);
if (!this._isMounted) return;
if (state === this.state.authState) { return; }

if (state === 'signedOut') { state = 'signIn'; }
Expand All @@ -103,13 +109,15 @@ export default class Authenticator extends React.Component {
const statesJumpToSignIn = ['signedIn', 'signedOut', 'loading'];
Auth.currentAuthenticatedUser()
.then(user => {
if (!this._isMounted) return;
if (user) {
this.handleStateChange('signedIn', null);
} else {
if (statesJumpToSignIn.includes(authState)) this.handleStateChange('signIn', null);
}
})
.catch(err => {
if (!this._isMounted) return;
logger.debug(err);
if (statesJumpToSignIn.includes(authState)) {
Auth.signOut()
Expand Down

0 comments on commit b218a72

Please sign in to comment.