verifying an email address through firebase using the verifyPasswordResetCode method

Currently in the process of setting up firebase authentication.

Instead of relying on firebase's default pages for recovery password and email verification, I want to handle these processes directly on my website.

To customize the recovery steps:

  1. Update the default email URL in firebase to redirect users to my domain
  2. In the code, ensure to check for the oobCode
 useEffect(() => {
   async function verifyToken() {
     try {
       const code = searchParams.get('code');
       if (code) {
         const email = await verifyPasswordResetCode(getAuth(), code);
         form.setFieldValue('email', email);
         form.setFieldValue('oobCode', code);
       } else {
         throw new Error('Missing oobCode');
       }
     } catch (e) {
       setError(e);
     } finally {
       setVerifying(false);
     }
   }

   setVerifying(true);
   verifyToken();
 }, []);
  1. Proceed with confirming password reset
  const submitHandler = useCallback(
    async ({ oobCode, password }: FormType) => {
      setLoading(true);
      try {
        await confirmPasswordReset(getAuth(), oobCode, password);
        setPasswordChange(true);
      } catch (e) {
        setError(e);
      } finally {
        setLoading(false);
      }
    },
    [setLoading]
  );

When it comes to email verification, there is still an oobCode that needs validation. However, the method to confirm this code seems unclear. Should I use verifyPasswordResetCode again or is there another function I should be utilizing?

Answer №1

As per the guidance provided in the custom email action handlers documentation, make use of applyActionCode.

  1. To handle email address verification, utilize the applyActionCode function. For instance:
function handleVerifyEmail(auth, actionCode, continueUrl, lang) {
  // Customize the UI based on the selected language determined by the lang parameter.
  // Attempt to apply the email verification code.
  applyActionCode(auth, actionCode).then((resp) => {
    // Email address has been confirmed.

    // TODO: Show a confirmation message to the user.
    // You can also offer a link back to the app for the user.

    // TODO: If there is a continue URL available, display a button that redirects the user back to the app via continueUrl with additional parameters from the URL.
  }).catch((error) => {
    // Code is invalid or expired. Prompt the user to verify their email address again.
  });
}

In the above example:

  • auth represents an instance of Firebase Auth obtained through getAuth().
  • actionCode corresponds to the oobCode GET parameter.
  • continueUrl and lang refer to their respective GET parameters.

It is advised to adhere to the documentation and also incorporate support for resetPassword and recoverEmail as needed.

Note: The complete code sample can be accessed on the Firebase Snippets GitHub repository. Certain methods are pseudo-code and should be adjusted to fit the framework you are utilizing.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JavaScript's getElementById function may return null in certain cases

I am studying JavaScript and I have a question about the following code snippet: document.getElementById('partofid'+variable+number). Why isn't this working? Check out these examples and JSfiddle link. I want the "next" button to remove th ...

The value of a variable undergoes a transformation following its insertion into an

During my coding tests, I came across this snippet of code: <script> var newData = {}, graphs = [] for(var j=0; j<2; j++){ newData["name"] = 'value '+ j console.log(newData["name"]); graphs.push(newData); console.log ...

What is the process for showing a table based on a selected value?

I could use some help - I am working on a <select> element with 4 different options in the form of <option>. What I want to achieve is to have tables that are initially not visible, and then when an option is clicked, the corresponding table wi ...

Script tag for Next.js reloading functionality

I have been facing a challenge while trying to integrate third-party commenting scripts like (disqus, remark42, hyvor) into my next.js application. The issue I encountered is that the script only loads on the initial page load. Subsequently, I need to refr ...

Unexpected token error occurs when making cross-domain AJAX requests to the server and receiving a JSON object

I have set up an express server to handle get requests to specific url endpoints. When responding to these requests, I am sending back data in JSON format to enable making Ajax calls and retrieving data from the page. To allow for cross-domain requests, I ...

Determining the total number of current connections using JavaScript, jQuery, and Selenium WebDriver

I need your assistance as I've encountered a roadblock. In my automated tests using Selenium WebDriver + Java, I rely on the following code snippet to check for active background AJAX connections: private boolean hasNoActiveConnections() { retur ...

Rendering images in Next.js version 10

Just recently, Next.js version 10 was launched featuring the latest Image element, making it a great asset for websites that heavily rely on images! When I receive an HTML response from the server, it looks something like this: HTML = "<div> &l ...

The issue of jQuery, Ajax, and MVC6 parameters becoming null after an Ajax request has been identified

Hello everyone, I am a beginner in asp.net core, c# and MVC 6. Currently, I am facing an issue with sending data over ajax to my controller. function sendAjaxData() { $.ajax({ url: "AjaxWithData", // using hardcoded value for testing purpose type ...

Should you hold off on moving forward until the asynchronous task finishes?

My goal is to retrieve location coordinates using the Google Maps JavaScript API in an asynchronous manner. Below is the function I've created for this purpose: function fetchCoordinates(address) { var geocoder = new google.maps.Geocoder(); ...

Delay fading in during the loading process

Recently, I came across a neat animation effect that I would love to incorporate into an upcoming project. The effect involves animating the opacity on load, also known as a fade in. I am curious if it is possible to link multiple elements together (for ...

End the div element upon completion of the Vimeo video

I am in need of a website that includes an intro video displayed as a full-width div over the background content. To achieve this, I created a Div containing an iframe video from Vimeo along with a button to skip the intro (which closes the div upon clicki ...

Efficient Ways to pass information to an Object within a nested function

const http = require('https'); exports.ip = async (req, res) => { const ip = req.body.ip; const ip_list = ip.trim().split(' '); const count = ip_list.length; var execution_count = 0; var success = {}; // **Creati ...

The objects-to-csv module encountered an error: fs.existsSync is not a valid function for this

"TypeError: fs.existsSync is not a function" Whenever I try to create a CSV file and input some data into it, this error message pops up. const objectstocsv = require('objects-to-csv'); export default { data () { return { ...

Is the append() function malfunctioning in jQuery?

Why is it copying two lines when I only want one line to be cloned on button click? Is there a way to make sure that only a single line is copied each time the button is clicked? Here is my code: $(function() { $('.add-more-room-btn').clic ...

A guide on exposing TypeScript classes globally through a WebPack bundle in JavaScript

Currently delving into TypeScript, my aim is to gradually replace JS with TS. However, due to having numerous JS files, my strategy involves creating new classes in TS and incorporating them into my existing JS files for the time being. Eventually, I plan ...

Exploring the capabilities of utilizing filters within a ternary operator in AngularJS

Can a filter be applied to a variable in the template within a ternary operation? <img ng-src="{{ image_url && image_url|filter:"foo" || other_url }}"> In this scenario, the filter is custom-made and I prefer not to alter it to accommodate ...

The error message thrown is: "TypeError - attempting to call an undefined function

I'm new to working with node and express, and I encountered an error when running my App. Does anyone have any solutions? I suspect the error may be in this line "app.use(express.static(user));" but I'm not certain. var express = require('e ...

Dynamic Next.js Redirects configured in next.config.js

After building the project, I am looking to update Redirects routes. Currently, we have redirect routes on our BE Api. My goal is to fetch these redirect routes dynamically and implement them in next.config.js. Here is what I have attempted: const getUrls ...

Is there a way to dynamically add <td> based on the quantity of values in a JSON object?

I have developed a program that retrieves values from JSON and I aim to display these values in a table. Currently, only the last array value is being displayed in the table even though all values are available in the console. The objective now is to dynam ...

Tips for ensuring all images are the same size within a div element

https://i.stack.imgur.com/EkmWq.jpg Is there a way to make sure all the images fit perfectly inside their respective border boxes without appearing stretched? I've tried setting fixed height and width within a div, but they always end up looking off. ...