Firebase Auth - createUserWithEmailAndPassword() - 在验证电子邮件之前阻止登录 [英] Firebase Auth - createUserWithEmailAndPassword() - prevent login until email is verified

查看:17
本文介绍了Firebase Auth - createUserWithEmailAndPassword() - 在验证电子邮件之前阻止登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用 createUserWithEmailAndPassword() 在 Firebase Auth 中创建一个用户帐户 - 无需登录用户.我希望用户首先验证电子邮件地址.直接登录用户会导致许多不需要的副作用.

I wish to create a user account in Firebase Auth using createUserWithEmailAndPassword() - without logging in the user. I wish for the user to verify the email address first. Signing in the user directly causes a lot of unwanted side effects.

/signup 页面具有以下代码 - 我希望用户在注册后停留在/signup 页面上能够看到注册消息.

The /signup page has the following code - I wish for the user to stay on the /signup page after registration to be able to see the registration message.

firebase.auth().createUserWithEmailAndPassword(data.email, data.password)
.then((user)=> {
  //Send email verification link
  user.sendEmailVerification()
  //Show message - "Your account was created - please verify using link in email"
  handleStatusMessage(AUTH_SUCCESS)
})
.catch((error)=>{...})

app.js 具有以下登录和重定向处理程序

The app.js has the following login and redirect handler

//handle login and redirect
firebase.auth().onAuthStateChanged((user) => {
  if (user) {
    if(!user.emailVerified){
      //Exit if email is not verified
      console.log('auth.js exit')
      //Line 7
    }
    store.dispatch(setInitialStore()).then(() => {
      renderApp()
      //Brings user to start page
      if (history.location.pathname === '/login') {
        history.push('/start')
      }
    })
} else {
  renderApp()
  //Brings user to /login or /verifyEmail page when logged out
  if(!history.location.pathname.includes('/verifyEmail')){
    history.push('/login')
  }  
}

我的问题是:

  1. 成功注册后用户将被重定向,除非我在第 7 行取消执行

  1. The user gets redirected after successful signup unless I cancel execution on line 7

如果我在第 7 行取消执行,用户在离开/signup 时会卡住

If I cancel execution on line 7, the user gets stuck when moving away from /signup

注销用户会导致 onAuthStateChanged() 触发两次 - 重定向用户

Logging out the user causes the onAuthStateChanged() to trigger twice - redirecting the user

如何在成功创建帐户后将用户保留在/signup 页面上,同时仍允许用户导航到/login/verifyEmail 位置?最好处于注销状态.

How can I keep the user on the /signup page after successful account creation while still allowing the user to navigate to the /login /verifyEmail location? Preferably in logged out state.

推荐答案

我最终做的是添加检查用户在注册期间何时登录和注销.

What I ended up doing was adding checks for when the user was logged in and logged out during signup.

signupPage.js

signupPage.js

firebase.auth().createUserWithEmailAndPassword(data.email, data.password)
.then((user)=> {
  //Login is triggered --> line 4 in app.js
  user.sendEmailVerification() //Send email verification
  handleStatusMessage(AUTH_SUCCESS) //Show success message
  firebase.auth().signOut() //Logout is triggered --> line 16 in app.js
})
.catch((error)=>{ ... }

app.js

//handle login and redirect
firebase.auth().onAuthStateChanged((user) => {
  if (user) {
    //** line 4 ** EXIT if logged in from /signup
    if(!isEmailVerified && history.location.pathname.includes('/signup')){
      return
    }
    store.dispatch(setInitialStore()).then(() => {
      renderApp()
      //Brings user to start page
      if (history.location.pathname === '/login') {
        history.push('/start')
      }
    })
} else {
  //** line 16 ** EXIT if logged out from /signup
  if(history.location.pathname.includes('/signup')){
    return
  }
  renderApp()
  //Brings user to /login or /verifyEmail page when logged out
  if(!history.location.pathname.includes('/verifyEmail')){
    history.push('/login')
  }  
}

真的希望有一个选项,createUserWithEmailAndPassword() 能够自动发送电子邮件验证电子邮件,并且登录用户避免这种猫捉老鼠的游戏代码.:)

I really wish there was a option for createUserWithEmailAndPassword() to be able to automatically send the email verification email and not logging in the user to avoid this kind of cat-and-mouse game code. :)

一种与 createUserWithEmailAndPasswordEmailVerificationRequired() 类似的单独方法,它自动发送电子邮件链接并且不登录用户也可以工作;)

A separate method along the lines of createUserWithEmailAndPasswordEmailVerificationRequired() that automatically sends the email link and does not sign in the user would also work ;)

这篇关于Firebase Auth - createUserWithEmailAndPassword() - 在验证电子邮件之前阻止登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆