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