代码之家  ›  专栏  ›  技术社区  ›  Aidan Ewen

如何使用Meteor登录用户

  •  1
  • Aidan Ewen  · 技术社区  · 8 年前

    我已经添加了 accounts-password useraccounts:unstyled

    我在页脚中包含了登录模板。html格式-

    {{#if showSignIn}}
        {{> atForm state="signIn"}}
    {{/if}}
    

    当应用程序启动时,我正在对用户的创建进行硬编码-

    import { Accounts } from 'meteor/accounts-base'
    
    if (!Acounts.findUserByEmail('aidan@gmail.com')) {
      Accounts.createUser({
        username: 'Aidan',
        email: 'aidan@gmail.com',
        password: 'securePassword'
      });
    }
    

    只是我不知道如何实际登录我的用户。我只想在表单中输入密码和电子邮件地址。这不起作用(表单错误显示“禁止登录”)。

    我已尝试添加以下行(与我的帐户创建代码位于同一文件中)-

    accountsServer.validateLoginAttempt(()=>{return true;});
    

    毫不奇怪,这没有任何作用。

    我尝试在页脚中添加一个提交事件。js文件-

    Template.footer.events({
      'submit form'(event) {
        console.log('submitted');
        const email = document.getElementById('at-field-email').value;
        const password = document.getElementById('at-field-password').value;
        Meteor.loginWithPassword(email, password);
      },
    });
    

    我可以看到活动正在启动,但当我尝试时 Meteor.user() 在控制台上 null .

    2 回复  |  直到 8 年前
        1
  •  1
  •   Aidan Ewen    8 年前

    有打字错误 if (!Acounts.findUserByEmail('aidan@gmail.com')) (本来应该是 Accounts ). 未创建用户。

    使用单个硬编码用户进行超级简单的功能登录-

    添加帐户密码包和ui用户帐户包。

    > meteor add accounts-password
    > meteor add useraccouts:unstyled
    

    帐户密码包处理实际登录。用户帐户:无样式的软件包为帐户管理提供了一组模板。

    然后将登录表单添加到模板中。

    <template name="templateWithLogin">
        {{> atForm state="signIn"}}
    </template>
    

    最后创建一个用户(例如,在 /server/main.js ).

    import { Accounts } from 'meteor/accounts-base';
    
    if (!Accounts.findUserByEmail('user@gmail.com')) {
      Accounts.createUser({
        username: 'User',
        email: 'user@gmail.com',
        password: 'securePassword'
      });
    }
    

    这应该是 每件事 需要获得一个功能性登录表单。有很多在线教程可用于创建其他功能。主要文档是 here .

        2
  •  -1
  •   kpie    8 年前

    您可以使用{{>loginButtons}}获取ui,使用{{#ifcurrentUser}}获得功能。