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

Question about Auth.currentUserInfo #358

Closed
ldgarcia opened this issue Feb 24, 2018 · 4 comments
Closed

Question about Auth.currentUserInfo #358

ldgarcia opened this issue Feb 24, 2018 · 4 comments
Labels
bug Something isn't working

Comments

@ldgarcia
Copy link
Contributor

ldgarcia commented Feb 24, 2018

Do you want to request a feature or report a bug

Not sure if it is a bug, or I am doing something wrong

What is the current behavior?

  1. Auth.currentUserInfo() returns the user information after the user signs in.
{
  attributes: { ... } // ok
  id: undefined // btw, why is this undefined?
  username: "johndoe" // ok
}
  1. If you reload the app, the user is still signed in (as determined by checkUser in Authenticator) but now Auth.currentUserInfo() returns an empty object.
{}

Logged error:

YellowBox.js:82 TypeError: Cannot read property 'identityId' of null
    at AuthClass.<anonymous> (Auth.js:781)
    at step (Auth.js:44)
    at Object.next (Auth.js:25)
    at fulfilled (Auth.js:16)
    at tryCallOne (core.js:37)
    at core.js:123
    at JSTimers.js:301
    at _callTimer (JSTimers.js:154)
    at _callImmediatesPass (JSTimers.js:202)
    at Object.callImmediates (JSTimers.js:470)

What is the expected behavior?

I think it should return the user information in both cases.

Code for reproducing the issue

import React from 'react';
import Amplify, { Auth } from 'aws-amplify';
import { withAuthenticator } from 'aws-amplify-react-native';
import { StyleSheet, Text, View, Button } from 'react-native';

Amplify.configure({
    Auth: {
        identityPoolId: '',
        region: 'us-east-1',
        userPoolId: '',
        userPoolWebClientId: '',
    }
})

class App extends React.Component {
  state = {
    info: {}
  }

  signOut = async () => {
    await Auth.signOut()
    this.setState({info: {}})
    this.props.onStateChange('signedOut')
  }

  async componentDidMount() {
    const info = await Auth.currentUserInfo()
    console.log('Returned info: ', info)
    this.setState({ info })
  }

  render() {
    const { info } = this.state
    const { username } = info
    return (
      <View style={styles.container}>
        <Text>Welcome, {username}</Text>
        <Button
          title="Sign Out"
          onPress={this.signOut}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

export default withAuthenticator(App)
@UnleashedMind UnleashedMind added the investigating This issue is being investigated label Feb 24, 2018
@manueliglesias
Copy link
Contributor

Hi @ldgarcia

I was able to replicate the behavior you are seeing, this is a bug in the library.

I was able to identify a workaround for you while we work on a bugfix:

Change your componentDidMount like this:

  async componentDidMount() {
    await (await Auth.currentCredentials()).getPromise(); // Wait for credentials

    const info = await Auth.currentUserInfo()
    console.log('Returned info: ', info)
    this.setState({ info })
  }

For context, the library tries to read the identityId from the credentials, those might not be ready yet when calling Auth.currentUserInfo()

@manueliglesias manueliglesias added bug Something isn't working and removed investigating This issue is being investigated labels Mar 1, 2018
@ldgarcia
Copy link
Contributor Author

ldgarcia commented Mar 5, 2018

Thanks @manueliglesias, I will try using the workaround.

@powerful23
Copy link
Contributor

Issue fixed in #331

@github-actions
Copy link

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants