所以我设法让passport-twitter与jsonwebtoken库一起工作,但为了使它正常工作,我必须使用express-session作为中间件.我不想添加会话因为我使用jsonwebtoken来返回令牌.
这是代码
autheticate.js
router.get('/twitter',function(req,res,next){ passport.authenticate('twitter',{session: false},function(err,user,info){ if(err){ return next(err); } if(user){ var token = createtoken(user); console.log(token); return res.json({token: token}); } else { return res.status(401).json(info); } })(req,next); });
我已经添加了session:false作为参数,但是在server.js上它保持吐出错误,我需要使用快速会话.
server.js
var express = require('express'); var path = require('path'); var logger = require('morgan'); var bodyParser = require('body-parser'); var mongoose = require('mongoose'); var passport = require('passport'); var session = require('express-session'); var config = require('./config'); mongoose.connect('mongodb://localhost',function() { console.log("Connected to the database"); }) require('./passport')(passport); var app = express(); var authenticate = require('./routes/authenticate')(app,express,passport); var api = require('./routes/api') (app,passport); // uncomment after placing your favicon in /public //app.use(favicon(__dirname + '/public/favicon.ico')); app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(session({ secret: config.TOKEN_SECRET,resave: true,saveUninitialized: true,})); app.use(express.static(path.join(__dirname,'public'))); app.use(passport.initialize()); app.use('/auth',authenticate); app.use('/api',api); app.get('*',res) { res.sendFile(__dirname + '/public/app/views/index.html'); }); app.listen(3000,function(err) { if(err) { return res.send(err); } console.log("Listening on port 3000"); });
所以每当我删除app.use(session())并尝试使用passport-twitter进行身份验证.我会得到这个错误
error Oauth Strategy requires app.use(express-session));