2014-09-03 69 views
2

我正在构建Meteor应用程序,并且需要在用户创建帐户后删除流星的自动注册。如何在注册流星后创建没有自动注册的用户

我正在使用用户界面的帐户密码和帐户条目(可选)。

有什么想法?谢谢。

+0

可能重复[如何防止创建用户后自动登录](http://stackoverflow.com/questions/17360037/how-to-prevent-auto-login-after-create-user) – 2014-09-03 10:57:29

+0

是的,它是重复的。对不起,我在创建之前还没有找到它。谢谢! – skozz 2014-09-03 11:03:58

回答

1

您可以使用下面的代码:

设置一个firstLogin标志上创建

Accounts.onCreateUser(function(options, user) { 
    user.firstLogin = true; 
    return user; 
}); 

方法来更新标志

Meteor.methods({ 
    updateUserFirstLogin: function(userId) { 
    Meteor.users.update({ 
     _id: userId 
    }, { 
     $set: { 
     'firstLogin': false 
     } 
    }); 
    } 
}); 

检查,如果用户在登录一个新

Accounts.validateLoginAttempt(function(attemptInfo) { 
    if (!attemptInfo.user) { 
    return false; 
    } 
    if (!attemptInfo.user.firstLogin) { 
    return true; 
    } else { 
    Meteor.call('updateUserFirstLogin', attemptInfo.user._id); 
    return false; 
    } 
}); 
8

这是通过电子邮件登录一个简单的解决方案,它将解除用户创建后autologins直到电子邮件地址验证拒绝后登录:

if (Meteor.isServer) { 

    Accounts.validateLoginAttempt(function(attemptInfo) { 

     if (attemptInfo.type == 'resume') return true; 

     if (attemptInfo.methodName == 'createUser') return false; 

     if (attemptInfo.methodName == 'login' && attemptInfo.allowed) { 
      var verified = false; 
      var email = attemptInfo.methodArguments[0].user.email; 
      attemptInfo.user.emails.forEach(function(value, index) { 
       if (email == value.address && value.verified) verified = true; 
      }); 
      if (!verified) throw new Meteor.Error(403, 'Verify Email first!'); 
     } 

     return true; 
    }); 

} 
+0

问题在于,代码不会创建用户。因为这个validateLoginAttemp()以某种方式在createUser()之前运行。当它在第二秒返回false时,流星不会注册新用户,既不发送电子邮件确认。 – 2017-10-27 16:44:05

0

我发现了一个更简单的方法:

Accounts.validateLoginAttempt((data) => { 
    let diff = new Date() - new Date(data.user.createdAt); 
    if (diff < 2000) { 
     console.info('New user created -- denying autologin.'); 
     return false; 
    } else { 
     return true; 
    } 
}); 

这看到用户刚刚创建,因此不会登录。