如何在Meteor中临时禁用用户

时间:2016-10-08 18:45:02

标签: meteor temporary meteor-useraccounts

我正在使用Meteor开发一个简单的应用程序来学习框架。我正在使用包含accounts-password包的accounts-base包。

用户将创建一个帐户,他们的电子邮件地址将作为登录用户名。这一切都完美无缺。现在我想把它提升到一个新的水平。

我希望有能力暂时禁止用户一段时间 - 让我们说一周。

使用accounts-password包可以实现此功能,还是存在另一个可以实现此功能的包?否则我怎么能自己实现这个功能呢?

1 个答案:

答案 0 :(得分:1)

如何在每个用户的users集合中使用类似isBanned标志的内容?这样,您在登录用户之前检查此标志。您可以通过在应用禁令时设置日期字段来进一步扩展此功能,然后可以计算用于查看禁令是否可以自动解除的已用时间。

db.users.findOne()
{
    [...]
    "username" : "superadmin",
    "profile" : {
        "isActive" : true,
        "createdBy" : "system",

        // is this user banned? 
        "isBanned" : false,

        "updatedAt" : ISODate("2016-10-07T17:33:42.773Z"),
        "loginTime" : ISODate("2016-10-07T17:25:44.068Z"),
        "logoutTime" : ISODate("2016-10-07T17:33:42.660Z")
    },
    "roles" : [
        "superAdmin"
    ]
}

您的登录表单事件可能如下:

Template.loginForm.events({

    'submit #login-form': function(event,template){
        event.preventDefault();

// Check for isBanned flag

if(Meteor.users.find({username: template.find("#userName").value,isBanned: false}) {
        Meteor.loginWithPassword(
            template.find("#userName").value,
            template.find("#password").value,
            function(error) {
                if (error) {
                    // Display the login error to the user however you want
                    console.log("Error logging in. Error is: " + error);
                    Session.set('loginErrorMessage', error.message);
                    Router.go('/');
                }
            }
        );
        Meteor.call('updateLoginTime');
        Router.go('loggedIn');
    },
}
相关问题