Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(aws-amplify-react-native): Update Hub listener for sign out events #5587

Merged
merged 8 commits into from Apr 30, 2020
48 changes: 38 additions & 10 deletions packages/aws-amplify-react-native/src/Auth/Authenticator.js
Expand Up @@ -80,23 +80,51 @@ export default class Authenticator extends React.Component {
}

onHubCapsule(capsule) {
const { channel, payload, source } = capsule;
if (channel === 'auth') {
this.checkUser();
const {
payload: { event },
data,
} = capsule;
amhinson marked this conversation as resolved.
Show resolved Hide resolved
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('state cannot be undefined');
amhinson marked this conversation as resolved.
Show resolved Hide resolved

logger.info(
'Inside handleStateChange Method current authState:',
amhinson marked this conversation as resolved.
Show resolved Hide resolved
this.state.authState
);
let nextAuthState;
if (state === 'signedOut') {
state = 'signIn';
nextAuthState = this._initialAuthState;
} else {
nextAuthState = state;
}

let nextAuthData = this.state.authData;
if (data !== undefined) {
nextAuthData = data;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the important bit here is the hub listener for signout and looks good.

We can clean nextAuthData and nextAuthState with ternary operator so this is readable and become easier later. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great call 👍 I just updated the PR

this.setState({ authState: state, authData: data, error: null });

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 (this.props.onStateChange) {
this.props.onStateChange(state, data);
}
Expand Down