[英]User is instantly logged out when logging in using firebase as authenticator on react
[英]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.