gpt4 book ai didi

javascript - 没有密码的 Meteor 帐户

转载 作者:行者123 更新时间:2023-11-28 06:15:05 24 4
gpt4 key购买 nike

我想在 Meteor 中创建一个帐户系统,用户无法创建自己的帐户,并且没有密码。本质上,用户可以通过提供用户帐户名(在我的例子中是整数字符串)来登录。我尝试使用帐户密码模块并修改它,但对我来说,如何做到这一点似乎并不明显。

现在,我有一个 meteor 方法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
});

添加denyUsers 集合的规则,以便人们无法直接从客户端添加或删除用户。

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中有一个用户,所以设置一个 session 变量。

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.");
}
}
});
}
});

在其他模板中,用户已经登录,获取

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;
});

关于javascript - 没有密码的 Meteor 帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36051809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com