Meteor:在创建帐户之前检查 Meteor.users 集合中不存在的电子邮件 [英] Meteor: check email does not exist in Meteor.users collection before creating an account

查看:46
本文介绍了Meteor:在创建帐户之前检查 Meteor.users 集合中不存在的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,用户可以在其中输入他们的电子邮件地址和密码到加入表单中.这会创建帐户,但我现在想进一步开发它.

I have a form where users enter their email address and password into a join form. This creates the account but I now want to develop it further.

client.js:

Template.joinForm.events({
    'submit .form-join': function(e, t) {
        e.preventDefault();
        var email = t.find('#email').value,
        password = t.find('#password').value,
        username = Random.id(),
        array = [],
        profile = {
            nameOfArray: array
        };
        Accounts.createUser({
            email: email,
            username: username,
            password: password,
            profile: profile
        }, function(error) {
            if (error) {
                alert(error);
            } else {
                Router.go('/');
            }
        });
    }
});

创建用户帐户之前,您如何:

Before creating a user account, how do you:

  1. 检查 Meteor.users 集合中是否不存在 joinForm 中的 email 变量.在服务器上处理这个?

  1. Check if the email variable from the joinForm does not already exist in the Meteor.users collection. Processing this on the server?

如果email确实存在,那么拒绝用户创建?

If email does exist, then reject user creation?

我看到了新功能,想知道我是否可以使用这个 http://docs.meteor.com/#/full/accounts_validatenewuser

I have seen the new function and wondering if I can use this http://docs.meteor.com/#/full/accounts_validatenewuser

Accounts.validateNewUser(function (user) {
    // pseudo if statement code
    if (email not exist) {
        // 1. create the user account and then
        Accounts.sendVerificationEmail(userId, [email])
    } else {
        throw new Meteor.Error(403, "email address is already registered");
    }
});

感谢您阅读本文.

我不清楚是使用 Accounts.createUser 还是 Accounts.onCreateUser 以及应该在 客户端 上使用哪个代码,以及在服务器上.我的目标是安全地建立帐户,因此,在此过程中,从控制台拒绝任何其他修改权限.

I'm unclear as to whether to use Accounts.createUser or Accounts.onCreateUser and which code should be on the client, and which on the server. My aim is to build the account securely, therefore, deny any other modification privileges during this process from the console.

推荐答案

现在在服务器上创建了额外的空数组 nameOfArray如果 允许创建帐户,即传递 validateNewUser 函数.当然,您可以添加更多验证检查,例如密码长度.

The extra empty array nameOfArrayis now created on the server if the account is allowed to be created, ie, passing the validateNewUser function. Of course, you can add more validation checks for example, password length.

client.js:

Template.joinForm.events({
    'submit .form-join': function(e, t) {
        e.preventDefault();
        var email = t.find('#email').value,
            password = t.find('#password').value,
            username = Random.id();
        Accounts.createUser({
            email: email,
            username: username,
            password: password,
            profile: profile
        }, function(error) {
            if (error) {
                alert(error.reason);
            } else {
                Router.go('/');
            }
        });
    }
});

server.js:

Accounts.onCreateUser(function(options, user) {
    var newEmail = user.emails[0].address;
    console.log(newEmail);
    var emailAlreadyExist = Meteor.users
        .find({"emails.address": newEmail}, {limit: 1})
        .count() > 0;
    console.log(emailAlreadyExist + ' already exists');
    if (emailAlreadyExist === true) {
        throw new Meteor.Error(403, "email already registered");
    } else {
        profile = options.profile;
        profile.nameOfArray = [];
        user.profile = profile;
        return user;
    }
});

这篇关于Meteor:在创建帐户之前检查 Meteor.users 集合中不存在的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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