【问题标题】:AWS Amplify: onStatusChange then render main pageAWS Amplify:onStatusChange 然后呈现主页
【发布时间】:2018-01-27 17:29:24
【问题描述】:

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

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>

        );
      }
    }

【问题讨论】:

    标签: javascript react-native aws-amplify


    【解决方案1】:

    如果你解决了,我希望它可以帮助其他人。

    我认为以下是比使用 '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' 调用它,则应该在每次身份验证状态更改时调用它。

    【讨论】:

      【解决方案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 状态,内部组件状态就会更新,这会导致该组件与您应用的其余部分一起重新呈现。

      【讨论】:

      • 谢谢,但是当您重新启动应用程序时如何处理身份验证状态?状态将通过在此类的初始构造方法中调用 this.setState 来重置
      • 几种方法。我会将 accessToken 存储在 redux 存储中并使用 redux-persist 持久化。在进入验证器时,检查 accessToken 的到期日期 - 如果未到期,则将状态设置为已登录并注册。
      猜你喜欢
      • 2017-11-14
      • 2017-02-27
      • 2022-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多