【发布时间】:2021-03-14 23:04:52
【问题描述】:
我在后端使用 react native 和 AWS。我正在尝试添加一项功能,允许用户在登录应用程序后更改密码。我相信我应该能够使用 Auth.changePassword 函数 (AWS doc) 做到这一点。由于某种原因,我收到用户名或密码不正确的错误消息。
测试的时候用户返回一个CognitoUser,oldPassword肯定是正确的,newPassword如果不符合字符设置就会报错。我不知道问题可能是什么。我还应该结合使用 completeChangePassword 吗?
import React from 'react';
import {Auth} from '@aws-amplify/auth';
export default class ChangePassword extends React.Component {
constructor(props) {
super(props);
this.state = {
oldPassword: '',
newPassword: '',
};
}
handleChangePassword = async () => {
const { oldPassword, newPassword } = this.state;
await Auth.currentAuthenticatedUser()
.then(user => {
return Auth.changePassword(user, oldPassword, newPassword)
})
.then(data => console.log(data))
.then(user => this.props.navigation.navigate('Home'))
.catch(err => console.log(err))
}
render() {
return (
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.text_header}>Change Password</Text>
</View>
<Animatable.View
animation='bounceInUp'
style={styles.footer}
>
<View style={styles.footer}>
<Text style={styles.text_footer}>Old Password</Text>
<View style={styles.action}>
<Feather
name='lock'
color='#05375a'
size={20}
/>
<TextInput
placeholder='Enter old password'
style={styles.textInput}
autoCapitalize='none'
onChangeText={(val) => this.setState({ oldPassword: val })}
/>
</View>
<Text style={styles.text_footer}>New Password</Text>
<View style={styles.action}>
<Feather
name='lock'
color='#05375a'
size={20}
/>
<TextInput
placeholder='Enter new password'
style={styles.textInput}
autoCapitalize='none'
onChangeText={(val) => this.setState({ newPassword: val })}
/>
</View>
<View style={styles.button}>
<TouchableOpacity
style={styles.signIn}
onPress={this.handleChangePassword}
>
<LinearGradient
colors={['#55B142', '#155843']}
style={styles.signIn}
>
<Text style={[ styles.textSign, { color: '#fff' }]}>Submit</Text>
</LinearGradient>
</TouchableOpacity>
</View>
<View style={{ alignItems: 'center'}}>
<View style={{flexDirection: 'row', alignItems: 'center', paddingTop: 5 }}>
<TouchableOpacity
onPress={() => this.props.navigation.navigate('EditProfile')}
style={[styles.signUp, {
//borderColor: '#155843',
//borderWidth: 1,
marginTop: 15,
}]}
>
<Text style={[styles.textSign, {
color: '#155843'
}]}>Go Back</Text>
</TouchableOpacity>
</View>
</View>
</View>
</Animatable.View>
</View>
);
}}
【问题讨论】:
标签: amazon-web-services authentication amazon-cognito aws-amplify change-password