Experience a new way to log in with signInWithPopup, leaving the Email

I am currently working on developing a registration form that utilizes Firebase auth for authentication.

There are two methods available for registering users:

  1. Using Email / Password Provider
  2. Using Google Auth Provider

Here is the process I follow:

Step 1: Initially, I register using the Email/Password provider and an account is created successfully.

Image after creating user with Email / Password

Step 2: Next, if a user tries to login using Google with the same email account using signInWithPopup, the current provider (Email/Password) gets replaced with Google. Subsequently, trying to log in using Email/Password authentication does not work because it has been replaced with Google.

What I need is:

  1. When attempting to create a user with signInWithPopup, check if the user already exists and immediately redirect to the dashboard without replacing the Email/Password Provider.

Or

  1. Show an error message stating
    Email is already registered using Email/Password. Try logging in using Email/Password

The project uses NextJS with Typescript and here is the full code snippet:

<!-- Code snippet omitted for brevity -->

Tried incorporating fetchSignInMethodsForEmail method within signInWithPopup but encountered unresolved issues.

Answer №1

The issue you are experiencing is operating as intended for specific email addresses from certain providers. For example, Google is a preferred provider for @gmail.com addresses. When signing into Google with an account that has a gmail address, it takes priority over other providers using the same address.

To avoid this problem, here are the steps to follow:

  1. Instruct the user to input their email address.
  2. Verify if the email address is already associated with your project by utilizing fetchSignInMethodsForEmail. This function will provide information on the known providers/sign-in methods linked to the email address.

Use this data to display the appropriate provider interface to the user so they can sign in to their existing account seamlessly.

Answer №2

After encountering the same error, I discovered that it was due to the email address not being verified at the time when the user attempted Google authentication.

To solve this issue, I implemented a system where an email verification link is sent to the user immediately after account creation (which requires user confirmation by clicking on the link).

By using either this link or a password reset link, the email address is verified. Subsequently, when a verified user attempts to use a different provider such as Google, it gets added as another provider without replacing the original email/password provider.

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

Step-by-step guide on inserting a variable into .doc() to create a new table

Recently, I created a SignIn page that uses variables to store data fetched with document.getElementByClassName. Now, I am facing an issue while trying to create a new document on Firebase using the person's name stored in a variable. Struggling with ...

MongoDB facing difficulties in updating the database

Seeking help with my MongoDB setup as I am still new to it. I have a database where card data is stored and need to update counts when users like cards, resulting in a total likes number. I'm facing an issue where I keep getting a 400 error response ...

Show information retrieved from one API request within another API request

Currently, I am in the process of retrieving data from the Youtube API by utilizing 2 separate requests. One request is used to fetch a list of videos, while the other request provides details for each individual video. The initial request successfully di ...

Issue with state not being properly updated in a separate component within React/Next.js framework

Having an issue I am in the process of creating a cart feature and have encountered an issue in my main _app.js file, essentially the index.js file for react where I am utilizing the useState hook. The problem arises when I pass a function to update a va ...

The persistence of authentication state in Next.js Firebase is inconsistent

I can't figure out why Firebase auth isn't persisting. My project is quite basic at this point, with a simple sign-in function: await signInWithEmailAndPassword(auth, email, password); In my layout.tsx, I have the onAuthStateChange: const unsubs ...

What is the process for inputting a predefined function into an interface?

In my project, I have a Locale interface that defines the properties of a locale for my component: interface Locale { src: string; alt: string; language: string; i18nFormat: string; } During debugging, I am using the built-in .toSource() function ...

Obscuring CSS class names for NextJS 14 using Instagram-inspired styling and SWC technology

It's interesting to see how Instagram Direct utilizes Tailwind CSS for their class names, but they have somehow obfuscated it to make it non-readable and prevent content crawling bots. I've searched for solutions online, but it seems like not man ...

Managing a scenario with a union type where the value can be retrieved from one of two different functions

There are two similar methods that I want to refactor to eliminate redundant code. The first function returns a single element, while the second function returns multiple elements: //returns a single element const getByDataTest = ( container: HTMLElement ...

Enhance your AJAX calls with jQuery by confidently specifying the data type of successful responses using TypeScript

In our development process, we implement TypeScript for type hinting in our JavaScript code. Type hinting is utilized for Ajax calls as well to define the response data format within the success callback. This exemplifies how it could be structured: inter ...

What is the way to assign a variable to ngClass in Angular?

I'm currently working on creating modals that will display different content based on which button is clicked. Each button should trigger a unique modal to appear, each with its own specific content inside. When a button is clicked, the 'active&a ...

Can TypeScript provide a way to declare that a file adheres to a particular type or interface?

Experimenting with different TypeScript styles has been quite enjoyable, especially the import * as User from './user' syntax inspired by node. I've been wondering if there is a way to specify a default type as well. Suppose I have an interf ...

React, NextJS, and Redux Encounter Local Storage Glitch

I'm currently working on an app that is experiencing a bug when storing data in the local storage of a browser. The variables are successfully sent to the local storage, however, every time the page is refreshed, these variables reset. I initially ex ...

Is there a way to restrict props.children.some to only accept image types?

Currently troubleshooting the following issue: An error is occurring: 'Property 'type' does not exist on type 'true | ReactChild | ReactFragment | ReactPortal'. Property 'type' does not exist on type 'string'. ...

Test failed due to missing Jest mocked data acquisition

Having an issue with my component that is supposed to load data from an API, but the test cannot find the element containing the data as it is mocked for testing purposes. component: import { useDispatch, useSelector } from "react-redux"; import ...

The process of setting permissions for a webview app to only access a middleware in next.js

Is there a way to restrict access to middleware in Next.js so that only mobile devices can access it? I need to handle certain DOM elements for a webview app, but for desktop web, I want them to go directly to the index page without passing through the mid ...

Leverage the power of Next.js to dynamically render a specific section of an HTML webpage

I want to incorporate Next.js into a specific section of my website, while the rest of the site is built using different frameworks that are not even based on React. I aim to use Next.js within a <div id="nextjs_div">...</div> element ...

Using Angular 4 for HTML 5 Drag and Drop functionality

I have been working on integrating native HTML 5 drag & drop functionality into my angular application. While I have successfully implemented the drag and dragOver events, I am facing an issue with the drop event not firing as expected. Here is the snipp ...

Is there a possibility that typescript decorators' features will be polyfilled in browsers lacking ES5 support?

According to the typescript documentation, a warning is issued: WARNING  If your script target is lower than ES5, the Property Descriptor will be undefined. If the method decorator returns a value, it will act as the Property Descriptor for the method. ...

Encountering an issue with the message: "Property 'ref' is not available on the type 'IntrinsicAttributes'."

Having trouble implementing a link in React and TypeScript that scrolls to the correct component after clicking? I'm using the useRef Hook, but encountering an error: Type '{ ref: MutableRefObject<HTMLDivElement | null>; }' is not assi ...

Accessing clipboard contents upon button click using TypeScript

Seeking assistance with retrieving data from the clipboard in TypeScript after clicking on a button. Please provide guidance. Thank you! ...