Javascript 在注册firebase之前,检查电子邮件是否真实

Javascript 在注册firebase之前,检查电子邮件是否真实,javascript,reactjs,firebase,firebase-authentication,Javascript,Reactjs,Firebase,Firebase Authentication,我正在做一个小项目,我确实完成了所有的认证工作,但有一件事,我想知道在进入注册过程之前如何检查电子邮件是否真实, 顺便说一句,我使用了react和Firebase,我确实在网上查找了一个名为email existence的软件包,我确实尝试过,它会返回true如果电子邮件是真的,false如果电子邮件确实存在,但当我使用react时它不起作用,它会返回一个错误 import firebase from '../util/firebase'; const emailExistence = requ

我正在做一个小项目,我确实完成了所有的认证工作,但有一件事,我想知道在进入注册过程之前如何检查电子邮件是否真实, 顺便说一句,我使用了
react
Firebase
,我确实在网上查找了一个名为
email existence
的软件包,我确实尝试过,它会返回
true
如果电子邮件是真的,
false
如果电子邮件确实存在,但当我使用react时它不起作用,它会返回一个错误

import firebase from '../util/firebase';
const emailExistence = require('email-existence');

export const normalSignup = (props, setSign, email, password, confirmPassword, username) => {
  emailExistence.check(email, function (error, response) { // return error here addresses.sort is not a function
    console.log('res: ' + response);
  });
}
不管怎样,我想知道是否有一种方法可以使用
Firebase
而无需提前使用外部软件包 PS:我没有使用云功能

可能只是使用一个来检查电子邮件是否有效

根据此JavaScript网页,您只需:

const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

if (emailRegex.test(email)) {
    console.log('Email valid!');
}
const emailRegex=/^([^()\[\]\\,;:\s@“]+(\.[^()\[\]\,;:\s@”]+*)(“+”)(\[[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]++];
if(emailRegex.test(电子邮件)){
log('Email valid!');
}

这不会阻止人们为不正确的域输入电子邮件,但可以确保如果有人使用不广为人知的邮件服务器,它也会被接受。

好吧,假设您想检查电子邮件是否是经过验证的电子邮件地址,您可以用以下方式编写代码

import firebase from '../util/firebase';
const App = {
  firebase: firebase,
  getLoggedInUser: () => {
    const currentUser = App.firebase.auth().currentUser
    if (currentUser) {
      return {
        email: currentUser.email,
        userId: currentUser.uid,
        isEmailVerified: currentUser.emailVerified
      }
    } else {
      return undefined
    }
  },
  isAuthenticated: () => {
    return (App.getLoggedInUser() && App.getLoggedInUser().isEmailVerified)
  },
  authenticate: async (email, password) => {
    await App.firebase.auth().signInWithEmailAndPassword(email, password)
  },
  signup: async (email, password) => {
    const userCredential = await App.firebase.auth().createUserWithEmailAndPassword(email, password)
    await userCredential.user.sendEmailVerification()
    return `Check your email for verification mail before logging in`
  },
这里发生了以下情况

  • 当用户注册时,将调用
    signup
    方法,并通过
    firebase
    发送电子邮件验证,如上述代码所示
  • 当用户登录时,
    authenticate
    方法被调用,因此根据
    firebase
    您已登录
  • 但是,要重定向或呈现某个页面,比如在登录后,您可以使用
    isAuthenticated
    方法向某个用户显示页面
  • 因此,您可以将方法
    isAuthenticated
    作为道具传递给
    react router
    ,并按照您的意愿呈现web应用程序
  • 这样,只有经过验证的真实电子邮件id才能访问您的应用程序
注意


此方法已在prod中运行,但它使用VueJS,并且是github上的一个开源项目。如果您想引用它,请告诉我您在客户端的唯一选择(如果您在Firebase上,我想您没有运行节点后端的奢侈),以获取与电子邮件存在类似的服务,该服务返回如果您
获取带有电子邮件地址的端点,则响应为“有效”或“无效”

这些通常都是优质服务,但如果你的流量很低,你可以尝试免费的服务。在我的例子中就是这样。 它们的端点可以这样调用(当然,如果您坚持使用客户端,这意味着任何人都可以通过浏览器网络选项卡从生产中窃取您的api密钥!):

返回一个JSON:

{
“电子邮件”:richard@example.com",
“你的意思是:”,
“用户”:“支持”,
“域”:“apilayer.net”,
“格式有效”:真,
“mx_发现”:正确,
“smtp_检查”:true,
“一网打尽”:错,
“角色”:没错,
“一次性”:假,
“自由”:假,
“分数”:0.8
}   
最好使用
分数
,其中:

[…]返回一个介于0和1之间的数字分数,反映所请求电子邮件地址的质量和可交付性

在反应中:

const[data,setData]=useState(null)
const[emailToVerify,setEmailToVerify]=useState('richard@example.com“)//只是为了举例
const apiKey=process.env.API_KEY
const fetchEmailVerificationApi=useCallback(异步()=>{
试一试{
const response=等待获取(`http://apilayer.net/api/check?access_key=${apiKey}&email=${emailToVerify}`)
const json=await response.json()
setData(json.score)//返回一个介于0和1之间的数字分数,反映请求的电子邮件地址的质量和可交付性。
}捕获(e){
控制台错误(e)
}
},[apiKey,emailToVerify])
useffect(()=>{
fetchEmailVerificationApi()
},[fetchEmailVerificationApi])

它看起来工作正常,您应该尝试找出错误所在。它是
地址。排序不是一个函数吗?
?是的,这正是错误所在,我在外部项目中尝试过,我在node中运行过它,它确实工作,但在react项目@RedBaronwell中它不工作。有些东西正在尝试运行
地址.sort
因此,请尝试并找出原因,或者为什么不使用此软件包?有更多的下载,这是一个很好的迹象,它使用良好,应该易于集成OK我将尝试@RedBaronI可以说这很容易检查它是否是一个有效的电子邮件模式,这里有一些类似的花式正则表达式:。尽管如此,他们检查它是否是一个有效的模式,不是有效的提供商。电子邮件拼写错误,如
john@gnail.com
将通过验证。如果你有mailchimp(它更多地用于目标受众),他们实际上有一个可靠的系统,告诉你,
gnail
是无效的(可能还有其他类似的断言,但我不知道)。我没有检查电子邮件的格式。兄弟,我想检查电子邮件是否真实,因为我已经检查了格式:)检查电子邮件是否真实的唯一方法是向其发送邮件,并且邮件不会反弹。显然,如果您发送邮件只是为了验证邮件是否真实,那么最好验证邮件是否属于您对于同一个用户-这基本上也是firebase的工作。一种广泛接受的方法是验证Adam提到的电子邮件格式,然后通过firebase向其发送验证邮件。不可能仅从前端检查电子邮件是否真的存在,因为浏览器没有直接发送电子邮件的API。建议by@frunkad,你应该使用Firebase发送一封验证电子邮件。这很酷,但这仍然不是我真正需要的,因为我认为
GET http://apilayer.net/api/check?access_key=YOUR_ACCESS_KEY&email=richard@example.com