1

I want to avoid automatically login after using createUserWithEmailAndPassword with React and Firebase. is that possible? Here is my code. Thanks a lot.

const SignUp = () => {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [displayName, setDisplayName] = useState("");
  const [error, setError] = useState(null);

  const createUserWithEmailAndPasswordHandler = async (event, email, password) => {
    event.preventDefault();

    try{
      const {user} = await auth.createUserWithEmailAndPassword(email, password);
      generateUserDocument(user, {displayName});
      const test = user.emailVerified;
      console.log("Verified: ",test,);
    }
    catch(error){
      setError('Error: ... '+error);
    };
    
    setEmail("");
    setPassword("");
    setDisplayName("");
               
            user.sendEmailVerification({url: process.env.REACT_APP_CONFIRMATION_EMAIL_REDIRECT="https://...my url.........."}); 
            console.log('Email is not verified '); 
            alert("email confirmation sent");

  };
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Agustin Rodrigues
  • 63
  • 1
  • 2
  • 11

1 Answers1

0

When you create an account, that account is automatically signed in. There is no way to prevent this.

The two most common reasons for asking this are:

  1. To prevent the user from using the app until they have verified their email address. In that case, you have two options.

    1. Use a passwordless email link to sign the user in. With this flow, the user's email address is automatically verified before they are signed in.
    2. Check whether the user's email address is verified before allowing them to use the app, and access its data. You'll do this both in the client-side application code, to control the navigation there, and in the server-side application code, or server-side security rules, to ensure the user can't access data they're not authorized for.
  2. Because your app's user is an application administrator who is creating accounts for other users.

    1. This is not a supported use-case in the client-side SDKs for Firebase, as such account creation should be done server-side through the Admin SDK.

    2. If you insist however, you can work around this limit by creating a secondary FirebaseApp instance to create the user, as shown here.

I'll add some links in a moment...

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807