0

我面临一个有趣的问题。当用户注册到应用程序时,它成功并且用户收到验证码到电子邮件并被带到确认帐户屏幕。当用户输入验证码并被带到应用程序的主屏幕时,就会发生这种情况。当用户从登录屏幕登录时,也会发生同样的事情。

在此处输入图像描述

这是我的login.js文件:

const login = async () => {
    let username = userName
    if (isEmailValid.test(username) && strongPassword.test(password)) {
      try {
        const user = await Auth.signIn(username, password).then(() => {
          loginToast()
          navigation.navigate('HomeTab')
          props.authActions.authAction(true)
        })
      } catch (error) {
        console.log('error signing in', error)
        loginErrorToast()
      }
    } else if (!isEmailValid.test(username) && !strongPassword.test(password)) {
      return loginErrorToast()
    }
  }

const mapDispatchToProps = (dispatch) => ({
  authActions: bindActionCreators(authAction, dispatch),
})

export default connect(null, mapDispatchToProps)(LoginScreen)

这在某种意义上是有效的,用户经过身份验证并登录但被注销,并且一旦用户进入主屏幕,redux 阶段也从 true 设置为 false。

这是confirmAccount.js

const confirmSignUp = async () => {
    try {
      await Auth.confirmSignUp(username, verificationCode).then(() => {
        confirmAccountToast()
        Auth.verifyCurrentUserAttribute(username)
        props.authActions.authAction(true)
        navigation.navigate('HomeTab')
      })
    } catch (err) {
      console.log({ err })
      confirmAccountErrorToast()
    }
  }

const mapDispatchToProps = (dispatch) => ({
  authActions: bindActionCreators(authAction, dispatch),
})

export default connect(null, mapDispatchToProps)(ConfirmScreen)

这是我drawer.js的退出功能所在。

  const signOut = async () => {
    try {
      await Auth.signOut().then(() => {
        signOutToast()
        props.authActions.authAction(false)
      })
    } catch (error) {
      console.log('failed to sign out: ', error)
      signOutErrorToast()
    }
  }

      {props.signedIn === false ? (
        <DrawerItem
          style={drawerItem}
          label='Kirjaudu'
          labelStyle={drawerLabel}
          icon={() => (
            <Ionicons
              name='ios-log-in-outline'
              size={30}
              color={colors.black}
            />
          )}
          onPress={() => props.navigation.navigate('Kirjaudu')}
        />
      ) : (
        <DrawerItem
          style={drawerItem}
          label='Kirjaudu ulos'
          labelStyle={drawerLabel}
          icon={() => (
            <Ionicons
              name='ios-log-out-outline'
              size={30}
              color={colors.black}
            />
          )}
          onPress={signOut()}
        />
      )}

const mapStateToProps = (state) => ({
  signedIn: state.authReducer.signedIn,
})

const mapDispatchToProps = (dispatch) => ({
  authActions: bindActionCreators(authAction, dispatch),
})

export default connect(mapStateToProps, mapDispatchToProps)(DrawerMenu)
4

1 回答 1

0

似乎问题只是一个结构性问题。我的意思是,例如这里的这一部分

    try {
      await Auth.confirmSignUp(username, verificationCode).then(() => {
        confirmAccountToast()
        Auth.verifyCurrentUserAttribute(username)
        props.authActions.authAction(true)
        navigation.navigate('HomeTab')
      })

需要是这样的。

    try {
      await Auth.confirmSignUp(username, verificationCode).then(() => {
        confirmAccountToast()
        Auth.verifyCurrentUserAttribute(username)
      }).then(() => {
        navigation.navigate('HomeTab')
      }).then(() => {
        props.authActions.authAction(true)
      })
于 2021-09-29T11:29:04.527 回答