簡體   English   中英

為什么當用戶登錄或確認注冊帳戶反應本機 aws 放大時,用戶會自動注銷?

[英]Why does user gets automatically logged out when user is logged in or confirms registered account react native aws amplify?

我面臨一個有趣的問題。 當用戶注冊到應用程序時,它成功並且用戶收到 email 的驗證碼並被帶到確認帳戶屏幕。 當用戶輸入驗證碼並被帶到應用程序的主屏幕時,就會發生這種情況。 當用戶從登錄屏幕登錄時,也會發生同樣的事情。

在此處輸入圖像描述

這是我的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,注銷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)

看來問題只是結構性的問題。 我的意思是,例如這里的這一部分

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM