【问题标题】:why firebase is not sending email confirmation to new user?为什么firebase不向新用户发送电子邮件确认?
【发布时间】:2021-01-06 16:25:51
【问题描述】:

我只是在用 react js 和 firebase 制作一个简单的单页应用程序。在认证中,部分用户可以使用邮箱和密码创建一个新账户,但要先登录,他们需要验证他们的邮箱。

我的代码。

useEffect(() => {
    const unsubscribe = auth.onAuthStateChanged((authuser) => {
      if (authuser){
        setuser(authuser);
      }
      else{
        setuser(null);
      }
    });

    return () => {
      unsubscribe();
    }

  },[user,username]);

  const signup = (event) => {
    event.preventDefault();
    var user = auth.createUserWithEmailAndPassword(email,password)
    .then((authuser) => {
      authuser.user.updateProfile({
        displayName : username
      })
    })
    .catch((error) => {
        alert(error.message);
      }
    );

    user.then((authuser) => {
        authuser.user.sendEmailVerification(); 
    })
    .then(() => {alert("We have send you an confirmation email to verify your account")})
    .catch((error) => { return; })
    .then(() => {handleClose()});
  }

  const signin = (event) =>{
    event.preventDefault();
    auth
    .signInWithEmailAndPassword(email,password)
    .then(() => {loghandleClose()})
    .catch((error) => {alert(error.message)})
  }

在创建新用户注册方法后跳过发送确认电子邮件部分,为什么用户在创建新帐户时会自动登录。 我认为 onAuthStateChanged() 功能会在新用户创建时自动运行,并跳过注册方法的发送确认电子邮件部分。

请问我该如何解决。

【问题讨论】:

    标签: javascript reactjs firebase-authentication


    【解决方案1】:

    我自己解决了这个我如何更改我的代码。

    useEffect(() => {
        const unsubscribe = auth.onAuthStateChanged((authuser) => {
          if (authuser && authuser.emailVerified === true){
            setuser(authuser);
          }
          else{
            auth.signOut();
            setuser(null);
          }
        });
    
        return () => {
          unsubscribe();
        }
    
      },[user,username]);
    
      const signup = (event) => {
        event.preventDefault();
        auth.createUserWithEmailAndPassword(email,password)
        .catch((error) => {
            alert(error.message);
          }
        )
        .then((authuser) => {
          authuser.user.updateProfile({
            displayName : username
          })
          authuser.user.sendEmailVerification(); 
        })
        .then(() => {alert("We have send you an confirmation email to verify your account")})
        .catch((error) => { alert(error.message) })
        .then(() => {handleClose()});
      }
    
      const signin = (event) =>{
        event.preventDefault();
        auth
        .signInWithEmailAndPassword(email,password)
        .catch((error) => {alert(error.message)})
        .then((user) => {
          if (!user.user.emailVerified){
            auth.signOut();
            alert("Please verify your email first");
          }
        })
        .then(() => {loghandleClose()})
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-22
      • 2011-10-27
      • 2011-07-19
      • 2023-03-14
      • 1970-01-01
      • 2020-08-06
      • 2018-08-14
      • 2020-06-03
      相关资源
      最近更新 更多