Skip to content

Commit

Permalink
Merge pull request #3024 from jessedoyle/gh-3021
Browse files Browse the repository at this point in the history
feat(aws-amplify-react-native): withOAuth Loading
  • Loading branch information
jordanranz authored May 13, 2019
2 parents 4e50c3d + 053079b commit af03407
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions packages/aws-amplify-react-native/src/Auth/withOAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default (Comp) => {
this.state = {
user: null,
error: null,
loading: false,
};

listeners.forEach(listener => Hub.remove('auth', listener));
Expand All @@ -50,11 +51,13 @@ export default (Comp) => {

componentDidMount() {
this._isMounted = true;
Auth.currentAuthenticatedUser().then(user => {
this.setState({ user })
}).catch(error => {
logger.debug(error);
this.setState({ user: null });
this.setState({ loading: true }, () => {
Auth.currentAuthenticatedUser().then(user => {
this.setState({ user, loading: false })
}).catch(error => {
logger.debug(error);
this.setState({ user: null, loading: false });
});
});
}
componentWillUnmount() {
Expand All @@ -72,19 +75,19 @@ export default (Comp) => {
case 'cognitoHostedUI': {
Auth.currentAuthenticatedUser().then(user => {
logger.debug('signed in');
this.setState({ user, error: null });
this.setState({ user, error: null, loading: false });
});
break;
}
case 'signOut': {
logger.debug('signed out');
this.setState({ user: null, error: null });
this.setState({ user: null, error: null, loading: false });
break;
}
case 'signIn_failure':
case 'cognitoHostedUI_failure': {
logger.debug('not signed in');
this.setState({ user: null, error: decodeURIComponent(payload.data) });
this.setState({ user: null, error: decodeURIComponent(payload.data), loading: false });
break;
}
default:
Expand All @@ -108,18 +111,19 @@ export default (Comp) => {
}

hostedUISignIn(provider) {
Auth.federatedSignIn({ provider });
this.setState({ loading: true }, () => Auth.federatedSignIn({ provider }));
}

signOut() {
return Auth.signOut().catch(error => logger.warn(error));
}

render() {
const { user: oAuthUser, error: oAuthError } = this.state;
const { user: oAuthUser, error: oAuthError, loading } = this.state;
const { oauth_config: _, ...otherProps } = this.props;

const oAuthProps = {
loading,
oAuthUser,
oAuthError,
hostedUISignIn: this.hostedUISignIn.bind(this, CognitoHostedUIIdentityProvider.Cognito),
Expand Down

0 comments on commit af03407

Please sign in to comment.