AWS Amplify:onStatusChange然后渲染主页面

问题描述 投票:0回答:2

当尝试进行身份验证状态更改时,我尝试渲染应用程序的主要入口点,但是当我尝试转到应用程序的主入口点时,我得到一个空白屏幕。我假设如果没有在渲染函数本身中调用它,我不能进入主页面?如果是这样,当我建立signIn时,如何渲染我的应用程序的主页面?

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);

Login.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>

        );
      }
    }
javascript react-native aws-amplify
2个回答
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 .../>); } }

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


0
投票

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

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

来自Amplify 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”调用它,则每次auth状态更改时都应调用它。

© www.soinside.com 2019 - 2024. All rights reserved.