繁体   English   中英

没有密码的流星帐户

[英]Meteor Accounts without Passwords

我想在Meteor中创建一个帐户系统,使用户无法创建自己的帐户,并且没有密码。 从本质上讲,用户将可以通过获得用户帐户名(在我的情况下为整数字符串)来登录。 我尝试使用accounts-pasword模块并对其进行修改,但对我来说这似乎并不明显。

现在,我有一个流星方法createPlayer

createPlayer: function() {
    var id = Math.floor(Math.random() * 10000000).toString();
    Accounts.createUser({ username: id, password: "test" });
    return id;
}

当服务器重新启动时,我称之为:

for (var i = 0; i < numberOfUsers; i++) {
        Meteor.call("createPlayer");
    }

此处的缺点是(1)用户仍然需要在默认密码“ test”中输入一个密码字段,并且(2)除那个用户外,还有一个按钮供用户创建新帐户我已经创建了。

编辑:解决方案必须使我能够仅发布与已登录用户有关的数据。 例如,使用帐户密码,我可以在发布中使用this.userId,以始终拥有最新的任务列表:

Meteor.publish("tasks", function () {
        var user = Meteor.users.findOne(this.userId);
        if (user && user.admin) {
            return Tasks.find({});
        }
        return Tasks.find({ user: this.userId });
    });

任何帮助表示赞赏。

如果您不想使用密码,则不需要使用accounts-password或任何程序包,只需创建一个集合并将用户插入该集合即可。 如果使用simple-schema ,则可以为集合定义模式

Users = new Mongo.Collection('userInfo'); //I am using userInfo because, users is used by accounts package

Users_Schema = new SimpleSchema({   
    username: {
        type: String,
        optional: false
    },
    firstName: {
        type: String,
        optional: false
    },
    lastName: {
        type: String,
        optional: false 
    },
    age: {
        type: Number,
        optional: false
    },
    //.. other details that you need.
});

Users.attachSchema(Users_Schema);

然后,您可以插入一个新用户,而不是Accounts.createUser ,可以检查username已存在并相应地引发错误。

Users.insert({
    username: "someUniqueUsername",
    firstName: "userFirstName",
    lastName: "userLastName",
    age: 20
});

deny规则添加到此“ Users集合,以便人们无法直接从客户端添加或删除用户。

Users.deny({
    insert: function () {
        return true;
    },
    update: function () {
        return true;
    },
    remove: function () {
        return true;
    }
});

更新

据我所知,如果没有使用accounts包的密码,就无法实现登录。 为了澄清您在评论中的第二点,请参阅以下内容。

对于登录表单HTML,请为用户名和登录按钮创建一个输入字段,

<template name="loginForm">
    <label for="username">Username:</label>
    <input id="username" type="text" /><br/>
    <input id="btnLogin" type="button" value="Login" />
</template>

在登录JS中,为登录按钮编写事件侦听器。 按下登录按钮后,获取userId并调用检查用户是否可用的服务器方法。 如果结果为true,则DB中有用户,因此设置会话变量。

Template.loginForm.events({
    'click #btnLogin': function (ev, template) {
        var userId = template.$('#username').val();
        //validate if userId is non-empty and valid userId before calling meteor method.
        Meteor.call('loginUser', userId, function (err, result) {
            if (err) {
                Session.set('userId', null);
                alert("There is an error while logging in.");
            } else {
                if (result === true) {
                    Session.set('userId', userId);
                    Router.go('someRouteAfterLogin'); //redirect to a different page.
                } else {
                    Session.set('userId', null);
                    alert("There is no such user.");
                }
            }
        });
    }
});

user已经登录的其他模板中,获取

Template.someTemplateAfterLogin.onCreated(function () {
    //Actually, this can go inside your Router's current route's data and waitOn but for demonstration purpose
    var userId = Session.get("userId");
    if (userId) {
        this.subscribe('userInfo', userId);
    } else {
         Router.go('login'); //or whatever name you specified for login
    }
});

在服务器端,

Meteor.methods({
    'loginUser': function (userId) {
        if (typeof userId == "string") {
            var user = Users.findOne({ username: userId });
            return user ? true : false;
        }
        return false;
    }
});

Meteor.publish('userInfo', function (userId) {
    if (typeof userId == "string") {
        return Users.find({ username: userId }); //use second parameter to restrict the fields that you want to publish with something like this return Users.find({ username: userId }, { fields: { username: 1, firstName: 1 } });
    }
    return null;
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM