如何使用Express/Node确认电子邮件地址? [英] How to confirm email address using express/node?

查看:61
本文介绍了如何使用Express/Node确认电子邮件地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为用户建立电子邮件地址验证,以验证他们的电子邮件是真实的.我应该使用哪个软件包来确认用户的电子邮件地址?到目前为止,我使用猫鼬和快递

I'm trying to build verification of email address for users, to verify their email is real. What package should I use to confirm the email address of the user? So far Im using mongoose and express

代码示例

var UserSchema = new mongoose.Schema({
    email: { type: String, unique: true, lowercase: true }
    password: String
});

var User = mongoose.model('User', UserSchema);

app.post('/signup', function(req, res, next) {
   // Create a new User
   var user = new User();
   user.email = req.body.email;
   user.password = req.body.password;
   user.save();
});

在app.邮政编码中,如何确认用户的电子邮件地址?

In the app.post codes, how do i confirm the email address of the user?

推荐答案

您要查找的内容称为帐户验证"或电子邮件验证".有很多Node模块可以执行此操作,但是原理如下:

What you're looking for is called "account verification" or "email verification". There are plenty of Node modules that can perform this, but the principle goes like this:

  • 您的用户模型应具有默认为 false active 属性
  • 当用户提交有效的注册表单时,创建一个新用户(其 active 最初为 false )
  • 使用密码库创建一个长随机字符串(通常是128个字符),并参考用户ID将其存储在数据库中
  • 使用散列将电子邮件发送到提供的电子邮件地址,该散列是指向服务器路由的链接的一部分
  • 当用户单击链接并点击您的路线时,请检查URL中传递的哈希值
  • 如果数据库中存在哈希,请获取相关用户并将其 active 属性设置为 true
  • 从数据库中删除哈希,不再需要
  • Your User model should have an active attribute that is false by default
  • When the user submits a valid signup form, create a new User (who's active will be false initially)
  • Create a long random string (128 characters is usually good) with a crypto library and store it in your database with a reference to the User ID
  • Send an email to the supplied email address with the hash as part of a link pointing back to a route on your server
  • When a user clicks the link and hits your route, check for the hash passed in the URL
  • If the hash exists in the database, get the related user and set their active property to true
  • Delete the hash from the database, it is no longer needed

您的用户现在已通过验证.

Your user is now verified.

这篇关于如何使用Express/Node确认电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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