1

当身份验证状态发生更改时,我试图呈现我的应用程序的主入口点,但是当我尝试转到应用程序的主入口点时,我得到一个空白屏幕。我假设如果没有在渲染函数本身中调用它,我就无法进入主页?如果是这样,当我建立登录时如何呈现我的应用程序的主页?

index.js

class App extends Component {

  /*
  constructor(props) {
    super(props);
    this.state = {
        authState: null,
        authData: null
    }
}
*/
handleAuthStateChange(state) {
  alert(state)
  if (state === 'signedIn') { 
    alert('here');
    return ( // go To Entry point of app
      <ApolloProvider store={store} client={client}>
      <AppWithNavigationState/>
      </ApolloProvider>
    );
  }
}

    render() {
      //const { authState, authData } = this.state;
     // const signedIn = (authState === 'signedIn');


        return (
         <Authenticator hideDefault={true} onStateChange={this.handleAuthStateChange}>
        <Login/>
        </Authenticator>

      );
    }

}
export default App = codePush(App);

登录.js

export default class Login extends Component {


    render() {

       const { authState, onStateChange } = this.props;

       if (!['signIn', 'signedOut', 'signedUp'].includes(authState)) {
           alert('login')
        return null;
       }


        return (<View style={styles.container}>

            <View style={styles.backgroundContainer}>
            <Image source={images.icons.LoginImage} style={styles.backgroundImage} />
            </View>
            <View style={styles.overlay}>
            <Button iconLeft light rounded style={styles.facebookLoginButton}>
            <Icon style={styles.facebookIcon} name='logo-facebook' />
            <Text style={styles.facebookButtonText}>Login with Facebook</Text>
            </Button>
            <Button rounded bordered light style={styles.loginLaterButton}
            onPress={() => onStateChange('signedIn')}>
            <Text style={styles.buttonText}>Sign In Test</Text>
            </Button>


            </View>

            </View>

        );
      }
    }
4

2 回答 2

3

如果你解决了它,我希望它会帮助别人。

我认为以下是比使用“onAuthStateChange”更好的选择:

来自放大 dox

import { Auth } from 'aws-amplify';

Auth.currentAuthenticatedUser({
    bypassCache: false  // Optional, By default is false. If set to true, this call will send a request to Cognito to get the latest user data
}).then(user => console.log(user))
.catch(err => console.log(err));

您可以在 '.then(user => ...' 中添加逻辑以添加到受保护页面的路由。您也可以从 '.catch(err => ' 重定向到登录页面。

如果您在函数中包含上述代码并从“componentWillReceiveProps”调用它,则每次身份验证状态更改时都应调用它。

于 2019-02-14T19:22:53.330 回答
2

这实际上是关于渲染和状态(与 AWS Amplify 无关)。首先,在构造函数中设置状态:

constructor(props) { super(props); this.state = { authState: '' }; }

然后,你的onAuthStateChange()变成:

onAuthStateChange(newState) { if (newState === 'signedIn') { this.setState({ authState: newState }); } }

最后,在您的render()方法中,您调整您的渲染,以便它根据您的身份验证状态执行“正确的操作”。

render() { if (this.state.authState === 'signedIn') { return (<ApolloProvider ...><MyApp/></ApolloProvider>); } else { return (<Authenticator .../>); } }

withAuthenticator()您也可以使用 HOC 将其抽象化(与AWS Amplify的 HOC 相同)。基本上,Authenticator 最初会显示出来。一旦收到 signedIn 状态,内部组件状态就会更新,这会导致该组件与应用程序的其余部分一起重新渲染。

于 2018-01-28T15:41:46.523 回答