meteor - 如何在客户端创建用户?

标签 meteor account

我正在为一家小公司从事实习文档共享项目。我想用 meteor 来做这个。我对 html/javascript 很熟悉,但对数据库不熟悉。 我的问题是处理用户。由于我在这里的研究,我不确定是否已经可以在客户端创建用户。官方文档展示了一些如何处理用户的方法,但没有示例。 我尝试在服务器端创建一个列表,如下所示:

Users = new Meteor.Collection("users");

然后我想在启动时插入一个用户,如下所示:

//on Client side
if (Meteor.isClient) {
var username = "My Name";

    Meteor.call("create_user", username, function(error, user_id) {
         Session.set("user_id", user_id);
});
}

//on Server side
if(Meteor.is_server) {
Meteor.methods({
    create_user: function(username) {
        console.log("CREATING USER");
        var USER_id = Users.insert({name: username});
        return user_id;
    },
});
}

但是读取 html 模板中的用户名不起作用...

有注册和登录的好例子吗?

干杯

最佳答案

在 Meteor 中添加帐户功能非常容易。可以是简单的电子邮件、密码,或者使用 facebook connect/twitter 等。

执行以下操作即可获得一个设置了用户帐户的简单 meteor 应用程序..

meteor create simpleapp
cd simpleapp

添加accounts-ui和accounts-password包

meteor add accounts-ui
meteor add accounts-password

您只需添加其他帐户相关的包即可实现 facebook/twitter/github/google 登录等

要列出其他可用的 meteor 包,请使用此命令

meteor list

现在编辑您的 simpleapp.html 文件以添加登录按钮等..

<head>
  <title>simpleapp</title>
</head>

<body>
  {{> hello}}
</body>

<template name="hello">
  <h1>Hello World!</h1>
  {{greeting}}
  <input type="button" value="Click" />
  {{loginButtons}}
</template>

我只是将 {{loginButtons}} 添加到默认 html 文件以添加默认登录按钮。

现在运行 meteor 应用程序并转到 localhost:3000

meteor

您无需做太多工作就实现了登录功能。 4-5行代码,它甚至可以处理忘记密码、注册新用户等事情

接下来你需要在用户登录时显示特定的 html。 您可以使用 {{currentUser}} 全局

来执行此操作

你相应地实现它

<template name="hello">
  <h1>Hello World!</h1>
  {{greeting}}
  <input type="button" value="Click" />
  {{loginButtons}}
  {{#if currentUser}}
    {{> loggedInTemplate}}
  {{else}}
    {{> loggedOutTemplate}}
  {{/if}}
</template>

<template name="loggedInTemplate">
  <!-- user is logged in -->
</template>
<template name="loggedOutTemplate">
  <!-- user is logged out -->
</template>

关于meteor - 如何在客户端创建用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14212028/

相关文章:

javascript - meteor 订阅未定义

javascript - 在 meteor 表格中使用集合助手

android - 在 AdMob 使用 Paypal

c++ - 在 Windows 7 操作系统上创建/删除新用户帐户

iis-6 - 证书 : Cannot find the certificate and private key for decryption Error when sign

meteor - 为什么使用accounts.ui.config 会导致我的应用程序崩溃?

javascript - Meteor JS - 按数组值排序集合

javascript - 插入失败: Error: Title is required

node.js - KeyStone JS 帐户 Controller

ios - 在 iOS 设备中获取默认用户电子邮件