Performing this task wasn’t easy.
To be honest, Google hasn’t documented very clearly the Firebase Authentification API. However, thanks to Stackoverflow (as always!), I manage to solve this and be able to update the e-mail address of an existing authenticated user (e-mail and password method) .
Introduction
While the API still provide a updateEmail
method, it doesn’t update a non-verified e-mail address. So instead, you need to perform a two-step process:
- The user makes a request to change the e-mail.
- The user confirms the e-mail modification.
How to create an e-mail account for testing purposes
To test that, you need to e-mail account.
For testing purposes only, I recommend Yopmail. It allows you to create throwaway e-mail addresses in seconds.
Just go to their website and use the random e-mail address generator.
It guarantees that you don’t have someone use the same e-mail address.
Be careful, though, if your e-mails contain sensitive information.
The code and workflow
Send the request to change the e-mail
The workflow may differ from an application to another, so I will only share the code that calls the Firebase Authentification API:
|
|
Confirm the e-mail modification
Once the user requested the verification, Firebase sends an e-mail to the new e-mail address.
While you can configure the e-mail template, in the free tier, you’re limited to what you customize.
Once the user clicks the link, he brought to a Firebase page. Once Firebase has completed the verification and update, the use can click Continue. This is where the continueUrl
is used.
In the demo application below, the custom continueUrl
allows to guide the user through the steps to complete the update.
While Firebase update the e-mail address in the users database, you probably need to update the Firestore document where you store the user’s other information.
Live demo
You can visit the project website I built while taking part in the Vueschool.io Masterclass (version 1).
To test it, you’ll need to:
- Create an account with a Yopmail e-mail address.
- Go to the View profile in the top right corner by clicking the user menu.
- Click Edit profile and change the e-mail address in the form.
For the next steps, I’ll let you follow the flow of actions.
You will need to open the Yopmail mailbox using the e-mail address provided in the update request. There, you’ll be able to open the verification e-mail.
The full code is available on GitHub.
I hope you enjoyed this article. If you have any question, as always, reach out and I’ll see if I can help you.
Credit: Hero photo by Mathyas Kurmann on Unsplash.