使用电子邮件与护照地方。 previous帮助不工作 [英] Use email with Passport-local. Previous help not working

查看:109
本文介绍了使用电子邮件与护照地方。 previous帮助不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我用护照的本地和前preSS来处理用户登录操作。到目前为止,我已经能够同时使用的用户名来获得成功登录,但用户名是很难记住,我个人不认为他们应该被用于处理用户,所以我试图修改所提供的passport-样本策略当地页以确认通过电子邮件的用户,但code不起作用。

策略我的电子邮件是:

  passport.use(新LocalStrategy(功能(电子邮件,密码,完成){
  User.findOne({电子邮件:电子邮件} {}功能(ERR,用户){
    如果(ERR){返回完成(ERR); }
    如果(!用户){返回完成(空,假,{消息:未知的用户+ E}); }
    user.comparePassword(密码功能(呃,isMatch){
      如果(ERR)回做(ERR);
      如果(isMatch){
        返回完成(NULL,用户);
      }其他{
        返回做(空,假,{消息:无效的密码'});
      }
    });
  });
}));

和它从这一战略源自:

  //登录策略
passport.use(新LocalStrategy(功能(用户名,密码,完成){
  User.findOne({用户名:用​​户名},功能(ERR,用户){    如果(ERR){返回完成(ERR); }
    如果(!用户){返回完成(空,假,{消息:未知用户名+}); }
    user.comparePassword(密码功能(呃,isMatch){
      如果(ERR)回做(ERR);
      如果(isMatch){
        返回完成(NULL,用户);
      }其他{
        返回做(空,假,{消息:无效的密码'});
      }
    });
  });
}));

所以用户名战略工作完全正常,但电子邮件一个不是。我甚至在这两个用户名和电子邮件策略留下的console.log(),看看有什么策略正在返回。用户名策略返回用户的所有信息,而邮件策略返回


  


。我不知道为什么该网站是这样做的,并且没有任何解决办法我到目前为止看到的工作。

一个解决方案我已经看到了另一篇文章认为,我只是用

  findOne({电子邮件:电子邮件},功能(ERR,用户){
});

选项与电子邮件登录查找用户,但它一直没有工作的。

下面是我用把用户输入的玉文件:

 延伸布局块内容
    H1登录    形式(动作='/登录,方法='后')
        p电子邮件
            BR
            输入#电子邮件(类型='文字',值='',占位符='@',名字=电子邮件)
        -p密码
            BR
            输入密码#(类型='密码',值='',占位符='密码',名字='密码')
        输入(类型=提交)提交

这里是POST输入:

  // POST /登录
//这是使用自定义回调一个替代实施
// acheive相同的功能。
exports.postlogin =功能(REQ,资源,下一个){
  passport.authenticate('当地',函数(ERR,用户信息){
    如果(ERR){返回下一个(ERR)}
    如果(!用户){
      req.session.messages = [info.message];
      返回res.redirect('/登录)
    }
    req.logIn(用户,功能(错误){
      如果(ERR){返回下一个(ERR); }
      返回res.redirect('/');
    });
  })(REQ,资源,下一个);
};

到底是什么怎么回事?随着电子邮件应该完美的工作。此外,由于我在问一个问题,无论如何,我怎么确认的电子邮件有人已登记后?


解决方案

在我看来,你只是在LocalStrategy回调电子邮件改变参数的名称。护照不会自动地知道该字段名为电子邮件虽然,你必须告诉它。


  

在默认情况下,LocalStrategy希望能够找到一个名为用户名和密码参数的凭据。如果您的网站prefers以不同的方式命名这些字段,选项更改默认值。


  passport.use(新LocalStrategy({
    usernameField:电子邮件,
    passwordField:passwd文件
  },
  功能(用户名,密码,完成){
    // ...
  }
));

来源

So I'm using passport-local and express to handle user log-in. So far I have been able to get a successful log-in while using usernames, but usernames are hard to remember and I personally don't think they should be used for handling users, so I tried modifying the sample Strategy provided on the passport-local page to confirm users via email, but the code doesn't work.

The strategy I have for emails is:

passport.use(new LocalStrategy(function(email, password, done) {
  User.findOne({ email: email }, {}, function(err, user) {
    if (err) { return done(err); }
    if (!user) { return done(null, false, { message: 'Unknown user ' + e }); }
    user.comparePassword(password, function(err, isMatch) {
      if (err) return done(err);
      if(isMatch) {
        return done(null, user);
      } else {
        return done(null, false, { message: 'Invalid password' });
      }
    });
  });
}));

And It's derived from this strategy:

//The login strategy
passport.use(new LocalStrategy(function(username, password, done) {
  User.findOne({ username: username }, function(err, user) {

    if (err) { return done(err); }
    if (!user) { return done(null, false, { message: 'Unknown user ' + username }); }
    user.comparePassword(password, function(err, isMatch) {
      if (err) return done(err);
      if(isMatch) {
        return done(null, user);
      } else {
        return done(null, false, { message: 'Invalid password' });
      }
    });
  });
}));

So the username strategy works perfectly fine, but the email one is not. I even left a console.log() in both the username and email strategy to see what the strategies were returning. The Username strategy returns all information on the user, while the email strategy returns

false

. I have no idea why the site is doing this, and none of the solutions I've seen so far work.

One solution I've seen on another post suggested that I just use the

 findOne({email: email}, function(err, user){
});

option to login with the email to find the user, but it hasn't been working at all.

Here is the Jade file I use to take input from the user:

extends layout

block content
    h1 Login

    form(action= '/login', method='post')
        p Email
            br
            input#email(type='text',value='',placeholder='@',name='email')
        p Password
            br
            input#password(type='password',value='',placeholder='Password',name='password') 
        input(type='submit') Submit

And here is the POST input:

// POST /login
//   This is an alternative implementation that uses a custom callback to
//   acheive the same functionality.
exports.postlogin = function(req, res, next) {
  passport.authenticate('local', function(err, user, info) {
    if (err) { return next(err) }
    if (!user) {
      req.session.messages =  [info.message];
      return res.redirect('/login')
    }
    req.logIn(user, function(err) {
      if (err) { return next(err); }
      return res.redirect('/');
    });
  })(req, res, next);
};

What the heck is going on here? With email it should be working perfectly. Also since I'm asking a question anyway, how do I confirm the email after somebody had registered?

解决方案

It seems to me you just changed the name of the argument in the LocalStrategy callback to "email". Passport doesn't automagically know that the field is named email though, you have to tell it.

By default, LocalStrategy expects to find credentials in parameters named username and password. If your site prefers to name these fields differently, options are available to change the defaults.

passport.use(new LocalStrategy({
    usernameField: 'email',
    passwordField: 'passwd'
  },
  function(username, password, done) {
    // ...
  }
));

Source

这篇关于使用电子邮件与护照地方。 previous帮助不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆