在使用 AWSAmplify 认证服务时,如果出现 “Username should be email” 的错误提示,可以通过调用 Amplify Auth 的 updateUsername 方法来将用户名更改为电子邮件地址。以下是一个示例代码:
import Amplify, { Auth } from 'aws-amplify';
Amplify.configure({
Auth: {
// 设置 Auth 配置
identityPoolId: 'YOUR_IDENTITY_POOL_ID',
region: 'YOUR_REGION',
userPoolId: 'YOUR_USER_POOL_ID',
userPoolWebClientId: 'YOUR_APP_CLIENT_ID'
}
});
// 将用户名更改为电子邮件地址
Auth.currentAuthenticatedUser()
.then(user => {
return Auth.updateUsername(user, 'YOUR_EMAIL_ADDRESS');
})
.catch(err => console.log(err));
如果需要更改的用户尚未进行身份验证,可以使用以下代码:
Auth.updateUserAttributes(user, { email: 'YOUR_EMAIL_ADDRESS'})
.then(result => console.log(result))
.catch(err => console.log(err));