Firebase身份验证-createUserWithEmailAndPassword()-在验证电子邮件之前禁止登录 [英] Firebase Auth - createUserWithEmailAndPassword() - prevent login until email is verified
问题描述
我希望使用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')
}
}
我的问题是:
-
成功注册后,用户将被重定向,除非我在第7行取消执行
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身份验证-createUserWithEmailAndPassword()-在验证电子邮件之前禁止登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!