Jukka Koivu
Jukka Koivu

Reputation: 319

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

I am facing an interesting problem. When user registers to the application it succeeds and user receives the verification code to the email and is taken to confirm account screen. There when user puts the verification code and is taken to the home screen of the application this happens. Same thing happens when user logs in from the login screen.

enter image description here

Here is my login.js file:

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)

This works in a sense that user is authenticated and logged in but gets logged out and redux stage is set also from true to false as soon user gets to home screen.

This is the 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)

And here is my drawer.js where the sign out function is.

  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)

Upvotes: 0

Views: 1141

Answers (1)

Jukka Koivu
Jukka Koivu

Reputation: 319

It seems that the problem was just a structural thing. What I mean by that is that for example this part here

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

Needed to be like this.

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

Upvotes: 0

Related Questions