使用jwt与护照得到404错误

时间:2018-03-03 19:07:10

标签: node.js express jwt passport-local

我尝试用Express,Passport,Jwt,本地护照和Mariadb创建一个身份验证api。 我不需要User模型,但想使用sql调用。

我遵循了一些教程:tuto1 tuto2 tuto3 但我总是得到404错误(POST请求)与卷曲和邮递员。 我不知道问题出在哪里。

app.js

<a href="http://example.com/cart/?add-to-cart=123">Buy</a>

passport.js

var express = require('express');
var logger = require('morgan');
var path = require('path');
var _ = require("lodash");
var favicon = require('serve-favicon');
var app = express();
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/routes')
var passport = require("passport");

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(passport.initialize());
app.use('/', routes);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
 });

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.json({ error: err });
});

module.exports = app;

routes.js

var passport    = require('passport');
var passportJWT = require("passport-jwt");

var ExtractJWT = passportJWT.ExtractJwt;

var LocalStrategy = require('passport-local').Strategy;
var JWTStrategy   = passportJWT.Strategy;

var connection = require('./database');
var sodium = require('./sodium');

passport.use('local-signin', new LocalStrategy({
usernameField: 'username',
passwordField: 'password'
} , function (req, username, password, done){
  if(!username || !password ) {
    return done(null, false);
    }
  connection.query("select * from users where username = ?", [username], 
 function(err, rows){
      console.log(err);
      console.log(rows);
    if (err)
    return done(null, error);
    if(!rows.length){
      return done(null, false);
    }
    var dbpassword = rows[0].password;
    if(sodium.password_check(dbpassword, password) == false){
        return done(null, false);
     }
       return done(null, rows[0]);
   });
  })
);

passport.use(new JWTStrategy({
    jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(),
    secretOrKey   : 'your_jwt_secret'
 },
 function (jwtPayload, cb) {
   connection.query("select * from users where id = ?", [jwtPayload.id], 
function(err, rows){
      console.log(err);
      console.log(rows);
    if (err)
    return done(null, error);
    if(!rows.length){
      return done(null, false);
    }
    var dbpassword = rows[0].password;
    if(sodium.password_check(dbpassword, password) == false){
        return done(null, false);
     }
       return done(null, rows[0]);
   });
  }
));

module.exports = passport ;

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

passport.use(&#39;本地登入&#39;,

passport.authenticate(&#39;本地&#39;,

这种不一致是一个问题吗?