Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仅在电子邮件和密码正确时登录用户?_Javascript_Html_Meteor_Account_Iron Router - Fatal编程技术网

Javascript 仅在电子邮件和密码正确时登录用户?

Javascript 仅在电子邮件和密码正确时登录用户?,javascript,html,meteor,account,iron-router,Javascript,Html,Meteor,Account,Iron Router,我有一个表单,当您单击submit时,无论发生什么情况,它都会转到路径/登录,即使您没有在表单中输入任何内容。但是,我希望它在转到/登录之前检查是否有输入电子邮件的用户,以及他们的密码是否正确。如果电子邮件未知或密码不正确,则我希望它显示错误(并停留在登录页面)。我一直在努力使这项工作,但我不知道如何做到这一点。有人知道怎么做吗?这是我目前掌握的代码: html: 你用iron router标记了这个,所以我假设你使用的是iron router包 通过在受限页面的所有路由上使用before钩子,

我有一个表单,当您单击submit时,无论发生什么情况,它都会转到路径/登录,即使您没有在表单中输入任何内容。但是,我希望它在转到/登录之前检查是否有输入电子邮件的用户,以及他们的密码是否正确。如果电子邮件未知或密码不正确,则我希望它显示错误(并停留在登录页面)。我一直在努力使这项工作,但我不知道如何做到这一点。有人知道怎么做吗?这是我目前掌握的代码:

html:


你用iron router标记了这个,所以我假设你使用的是iron router包

通过在受限页面的所有路由上使用before钩子,可以防止未经身份验证的用户访问登录页面以外的任何页面。before钩子将检查
Meteor.user()
,如果它不返回对象,则没有用户登录,它可以重新路由到登录页面

查看铁路由器文档,这里是关于挂钩前后的部分。它甚至显示了使用before钩子作为过滤器来防止未经身份验证的用户进入路由

它看起来像这样:

Router.map(function () {
  this.route('postShow', {
    path: '/posts/:_id',

    before: function () {
      if (!Meteor.user()) {
        // render the login template but keep the url in the browser the same
        this.render('login');

        // stop the rest of the before hooks and the action function 
        this.stop();
      }
    },

    action: function () {
      // render the main template
      this.render();

      // combine render calls
      this.render({
        'myCustomFooter': { to: 'footer' },
        'myCustomAside': { to: 'aside' }
      });
    },

    after: function () {
      // this is run after our action function
    }
  });
});

你的问题:“在我检查电子邮件/通行证是否有效之前,我如何检查电子邮件/通行证是否有效?”哦,我的问题应该是“在他们进入网站主页之前,我如何检查电子邮件/通行证是否有效?”谢谢,我想这是我需要的。我将不得不玩它来得到我想要的,但我认为这将为我工作。
// Sign In Template
Template.signIn.events({
    'submit #signInForm': function(e, t) {
        e.preventDefault();

        var signInForm = $(e.currentTarget),
            email = trimInput(signInForm.find('.email').val().toLowerCase()),
            password = signInForm.find('.password').val();

        if (isNotEmpty(email) && isEmail(email) && isNotEmpty(password) && isValidPassword(password)) {
            Meteor.loginWithPassword(email, password, function(err) {
                if (err) {
                    Session.set('alert', 'We\'re sorry but these credentials are not valid.');
                } else {
                    Sesson.set('alert', 'Welcome back New Meteorite!');
                }
            });
        }
        return false;
    },
});
Router.map(function () {
  this.route('postShow', {
    path: '/posts/:_id',

    before: function () {
      if (!Meteor.user()) {
        // render the login template but keep the url in the browser the same
        this.render('login');

        // stop the rest of the before hooks and the action function 
        this.stop();
      }
    },

    action: function () {
      // render the main template
      this.render();

      // combine render calls
      this.render({
        'myCustomFooter': { to: 'footer' },
        'myCustomAside': { to: 'aside' }
      });
    },

    after: function () {
      // this is run after our action function
    }
  });
});