护照局部淋巴结智威汤逊 - 简单 [英] passport-local with node-jwt-simple

查看:209
本文介绍了护照局部淋巴结智威汤逊 - 简单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何能结合护照到当地认证成功返回JWT令牌?

How can I combine passport-local to return a JWT token on successful authentication?

我想用节点智威汤逊 - 简单看着的passport.js 我不知道如何去。

I want to use node-jwt-simple and looking at passport.js I am not sure how to go about.

var passport = require('passport')
  , LocalStrategy = require('passport-local').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: 'Incorrect username.' });
      }
      if (!user.validPassword(password)) {
        return done(null, false, { message: 'Incorrect password.' });
      }
      return done(null, user);
    });
  }
));

是否有可能返回令牌调用完成时()?
事情是这样的......(只是伪code)

Is it possible to return the token when calling done() ? Something like this... (just pseudo code)

if(User.validCredentials(username, password)) {
  var token = jwt.encode({username: username}, tokenSecret);
  done(null, {token : token}); //is this possible?
}

如果不是,我怎么能返回令牌?

If not, how can I return the token?

推荐答案

我想它了!

所有你需要实施正确的战略的首位。在我的情况LocalStrategy,你需要提供你的验证逻辑。例如为了让我们用一个在护照地方。

First of all you need to implement the correct strategy. In my case LocalStrategy, and you need to provide your validation logic. For example sake let's use the one in passport-local.

var passport = require('passport')
  , LocalStrategy = require('passport-local').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: 'Incorrect username.' });
      }
      if (!user.validPassword(password)) {
        return done(null, false, { message: 'Incorrect password.' });
      }
      return done(null, user);
    });
  }
));

校验回调您提供功能(用户名,密码,完成)会照顾找到你的用户和检查,如果密码匹配(超出了问题的范围我的回答)

the verify call back you provide function(username, password, done) will take care of finding your user and checking if the password matches (beyond the scope of the question and my answer)

passport.js预计几件为它工作,一个是你在战略回报用户。我试图改变code的一部分,这是错误的。回调预计如果验证失败和对象(合法用户),如果您是成功的。

passport.js expects several pieces for it to work, one is that you return the user in the strategy. I was trying to change that part of the code, and that was wrong. The callback expects false if the validation fails and an object (the validated user) if you are successful.

现在....如何整合智威汤逊?

Now.... how to integrate JWT?

在登录路线,你将不得不处理一个成功的身份验证或不成功的。正是在这里,你需要添加JWT令牌创建。像这样:

In your login route you will have to handle a successful auth or an unsuccessful one. And it is here that you need to add the JWT token creation. Like so:

(记住禁用会话,否则你将必须实现序列化和反序列化功能。你不需要这些,如果你不坚持会话,你不是,如果你使用的是基于令牌身份验证)

(remember to disable the session, otherwise you will have to implement the serialize and deserialize functions. And you don't need those if you are not persisting the session, which you are not if you are using a token based auth)

从Passport本地例子:(添加了智威汤逊令牌)

From passport-local examples: (with the JWT token added)

// POST /login
//   This is an alternative implementation that uses a custom callback to
//   achieve the same functionality.
app.post('/login', function(req, res, next) {
  passport.authenticate('local', function(err, user, info) {
    if (err) { return next(err) }
    if (!user) {
      return res.json(401, { error: 'message' });
    }

    //user has authenticated correctly thus we create a JWT token 
    var token = jwt.encode({ username: 'somedata'}, tokenSecret);
    res.json({ token : token });

  })(req, res, next);
});

这就是它!现在,当你调用/登录后,用户名和密码(应该始终是通过SSL)之上的第一个code段将尝试根据您提供的用户名来查找用户,然后检查密码匹配(当然你将需要更改,以满足您的需求)。

And that is it! Now when you call /login and POST username and password (which should always be over SSL) the first code snippet above will try to find a user based on the username you provided and then check that the password matches (Of course you will need to change that to suit your needs).

在您的登录路线将被调用,在那里,你可以返回一个错误或有效令牌的照顾。

After that your login route will be called and there you can take care of returning an error or a valid token.

希望这会帮助别人。我若犯任何错误或忘带了什么东西让我知道。

Hope this will help someone. And if I have made any mistakes or forgot something let me know.

这篇关于护照局部淋巴结智威汤逊 - 简单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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