Skip to content

Commit 55f1819

Browse files
authored
fix: add section to clarify steps to update Cognito user email address with example (aws-amplify#4476)
* fix: add section to clarify steps to update Cognito user email address with example Related to: aws-amplify/amplify-js#987 aws-amplify/amplify-cli#10773 * remove extra space
1 parent 447bce5 commit 55f1819

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

src/fragments/lib/auth/js/manageusers.mdx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,73 @@ If you change the email address, the user will receive a confirmation code. In y
179179
let result = await Auth.verifyCurrentUserAttributeSubmit('email', 'abc123');
180180
```
181181

182+
## Updating and verifying a Cognito user email address
183+
184+
Updating an email address using `Auth.updateUserAttributes` will require a verification of the new email address. The user will be emailed a verification code to the updated email address and your application will need to receive the verification code and send it to `Auth.verifyCurrentUserAttributeSubmit` before the email address will be updated in Cognito.
185+
186+
The React application below implements this flow of updating the email address for the current user when the "Update Email" button is clicked, then provides a form to capture the verification code sent to the user.
187+
188+
```javascript
189+
// App.js
190+
import { Authenticator } from "@aws-amplify/ui-react";
191+
import '@aws-amplify/ui-react/styles.css';
192+
import { Auth } from "aws-amplify";
193+
import { useState } from "react";
194+
195+
async function updateUserEmail () {
196+
const user = await Auth.currentAuthenticatedUser();
197+
198+
await Auth.updateUserAttributes(user, {
199+
'email': '[email protected]',
200+
}).then(() => {
201+
console.log('a verification code is sent');
202+
}).catch((e) => {
203+
console.log('failed with error', e);
204+
});
205+
}
206+
207+
async function verifyEmailValidationCode (code) {
208+
await Auth.verifyCurrentUserAttributeSubmit('email', code)
209+
.then(() => {
210+
console.log('email verified');
211+
}).catch((e) => {
212+
console.log('failed with error', e);
213+
});
214+
}
215+
216+
function ValidationCodeForm() {
217+
const [validationCode, setValidationCode] = useState(null)
218+
return(
219+
<div>
220+
<label>
221+
Verification Code (sent to the new email):
222+
<input onChange={(e) => { setValidationCode(e.target.value)}} type="text" name="vc" />
223+
</label>
224+
<button onClick={() => verifyEmailValidationCode(validationCode)}>Send Code</button>
225+
</div>
226+
)
227+
}
228+
229+
export default function App() {
230+
const [showValidationCodeUI, setShowValidationCodeUI] = useState(false)
231+
232+
return (
233+
<Authenticator>
234+
{({ signOut, user }) => (
235+
<div className="App">
236+
<div>
237+
<pre>{JSON.stringify(user.attributes, null, 2)}</pre>
238+
</div>
239+
{showValidationCodeUI === false && <button onClick={() => updateUserEmail() && setShowValidationCodeUI(true)}>Update Email</button>}
240+
{showValidationCodeUI === true && <ValidationCodeForm />}
241+
<button onClick={signOut}>Sign out</button>
242+
</div>
243+
)}
244+
</Authenticator>
245+
);
246+
}
247+
```
248+
182249
## Managing security tokens
183250

184251
> When using Authentication with AWS Amplify, you don't need to refresh Amazon Cognito tokens manually. The tokens are automatically refreshed by the library when necessary.

0 commit comments

Comments
 (0)