Skip to content

Commit

Permalink
fix(aws-amplify-react-native): Update Hub listener for sign out events (
Browse files Browse the repository at this point in the history
#5587)

* update Hub listener for sign out

* refactor handleStateChange variables

Co-authored-by: Alex Hinson <alehin@amazon.com>
Co-authored-by: Eric Clemmons <eric@smarterspam.com>
Co-authored-by: Ashika <35131273+ashika01@users.noreply.github.com>
  • Loading branch information
4 people committed Apr 30, 2020
1 parent 8f03e22 commit b8d2c4d
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions packages/aws-amplify-react-native/src/Auth/Authenticator.js
Expand Up @@ -80,23 +80,44 @@ export default class Authenticator extends React.Component {
}

onHubCapsule(capsule) {
const { channel, payload, source } = capsule;
if (channel === 'auth') {
this.checkUser();
const {
payload: { event, data },
} = capsule;
switch (event) {
case 'cognitoHostedUI':
return this.handleStateChange('signedIn', data);

case 'cognitoHostedUI_failure':
case 'parsingUrl_failure':
case 'signOut':
case 'customGreetingSignOut':
return this.handleStateChange('signIn', null);
}
}

handleStateChange(state, data) {
logger.debug('authenticator state change ' + state);
if (!this._isMounted) return;
if (state === this.state.authState) {
return;
if (state === undefined)
return logger.info('Auth state cannot be undefined');

logger.info(
'Inside handleStateChange method current authState:',
this.state.authState
);

const nextAuthState =
state === 'signedOut' ? this._initialAuthState : state;
const nextAuthData = data !== undefined ? data : this.state.authData;

if (this._isMounted) {
this.setState({
authState: nextAuthState,
authData: nextAuthData,
error: null,
});
logger.log('Auth Data was set:', nextAuthData);
logger.info(`authState has been updated to ${nextAuthState}`);
}

if (state === 'signedOut') {
state = 'signIn';
}
this.setState({ authState: state, authData: data, error: null });
if (this.props.onStateChange) {
this.props.onStateChange(state, data);
}
Expand Down

0 comments on commit b8d2c4d

Please sign in to comment.