[英]req.user undefined after twitter authentication using express sever, passport.js
社會認證后,有很多問題與未定義req.user有關,但我發現沒有任何問題可以幫助我。
我已經能夠成功使用將護照用於Twitter身份驗證的示例: https : //github.com/passport/express-4.x-twitter-example 。 我試圖盡可能地遵循這種模式,但是似乎無法使其正常工作。
具體來說,我能夠成功進行身份驗證,但是req.user是未定義的。 這對我來說毫無意義,因為示例中返回的我的用戶數據沒有問題。
我不傾向於相信這是一個中間件問題(就像其他人一樣),因為中間件與示例中使用的相同。 可能有多個域,但是我不確定。 所有這些都在本地主機上完成。
在Twitter中,該應用程序已設置為網站為:127.0.0.1:3000/signin,回調URL為:127.0.0.1:2999/auth/twitter/return
如您所知,我的客戶端正在使用端口3000,並且正在對在端口2999上運行的服務器進行呼叫。
為了簡要介紹代碼,位於127.0.0.1:3000/signin上的客戶端具有一個鏈接到127.0.0.1:2999/auth/twitter的按鈕,從而啟動了身份驗證請求。 在后台,快速服務器是在server / index.js--server中創建的。 這會將路由導入到route / index.js中,其中的某些路由由controller authenticate.js處理。 如您所見,oauth twitter請求是在authenticate.js中提出的。 再次,身份驗證成功進行,我被重定向到127.0.0.1:3000/search。 但是,正如您在this.twitter_callback中所看到的,我正在打印req.user,它是未定義的。
請注意,我已經從代碼中刪除了使用者密鑰/秘密。
服務器/ index.js
var cors = require('cors')
var bodyParser = require('body-parser')
var express = require('express');
var app = express();
var http = require('http').Server(app)
var io = require('socket.io')(http)
// NOT SURE WHY I NEED TO GO BACK 3 FOLDERS TO GET TO PORT_CONFIG
var port = require("../port_config.json").server_port;
var PORT = Number(process.env.PORT || port);
var routes = require('./routes/index.js')
var database = require('./database/db.js')
var db = new database()
app.use(cors()); // middleware that allows cross-platform requests
app.use(bodyParser.json());
db.dbConnect(function(err,db_instance){
// routes
routes(app, db_instance, io)
// send user polls on connection
// TEMPORARY (WILL BE GRABBED ON LOGIN)
var user = null // WILL BE SET AFTER LOGIN
io.on('connection', function(socket) {
var places_attending = db_instance.collection('places_attending')
places_attending.find({}).toArray(function(err,docs){
var places_user_attending = docs.map(doc => {
if (doc.attending.indexOf(user) !== -1) {
return {
id: doc.id,
name: doc.name,
num_attending: doc.attending.length
}
}
})
socket.emit('places_user_attending', places_user_attending);
})
})
})
http.listen(PORT, function () {
console.log('Backend server listening at http://localhost:' + PORT);
})
module.exports = http
路線/index.js
var Search = require('../controllers/search.js')
var Add = require('../controllers/add.js')
var Authenticate = require('../controllers/authenticate.js')
module.exports = function(app, db, io) {
var search = new Search(db, io)
var add = new Add(db, io)
var authenticate = new Authenticate(app)
app.route('/api/search')
.post(search.search_yelp)
app.route('/api/add')
.post(add.add_attendee)
app.route('/auth/twitter')
.get(authenticate.twitter_authentication)
app.route('/auth/twitter/return')
.get(authenticate.twitter_callback)
}
authenticate.js
function authenticate(app) {
var passport = require('passport');
var Strategy = require('passport-twitter').Strategy;
// Configure the Twitter strategy for use by Passport.
passport.use(new Strategy({
consumerKey: REDACTED,
consumerSecret: REDACTED,
callbackURL: 'http://127.0.0.1:2999/auth/twitter/return'
},
function(token, tokenSecret, profile, cb) {
// In this example, the user's Twitter profile is supplied as the user
// record. In a production-quality application, the Twitter profile should
// be associated with a user record in the application's database, which
// allows for account linking and authentication with other identity
// providers.
return cb(null, profile);
}));
// Configure Passport authenticated session persistence.
passport.serializeUser(function(user, cb) {
cb(null, user);
});
passport.deserializeUser(function(obj, cb) {
cb(null, obj);
});
// Use application-level middleware for common functionality, including
// logging, parsing, and session handling.
app.use(require('morgan')('combined'));
app.use(require('cookie-parser')());
app.use(require('body-parser').urlencoded({ extended: true }));
app.use(require('express-session')({ secret: 'keyboard cat', resave: true, saveUninitialized: true }));
// Initialize Passport and restore authentication state, if any, from the
// session.
app.use(passport.initialize());
app.use(passport.session());
this.twitter_authentication = passport.authenticate('twitter')
this.twitter_callback = (
passport.authenticate('twitter', { failureRedirect: 'http://127.0.0.1:3000/signin' }),
function(req, res) {
console.log('REQ.USER OBJECT: ' + req.user)
res.redirect('http://127.0.0.1:3000/search');
}
)
}
module.exports = authenticate
任何幫助將不勝感激。
問題在於如何指定我的twitter_callback路由。
如果我將回調更改為此:
this.twitter_callback = app.get('/auth/twitter/return',
passport.authenticate('twitter', { failureRedirect: 'http://127.0.0.1:3000/signin' }),
function(req, res) {
console.log(req.user)
res.redirect('http://127.0.0.1:3000/search');
})
一切正常。 我認為這與我最初編寫中間件時未正確應用中間件有關。 雖然不確定在不使用twitter_callback的app.get的情況下如何重寫它以將其導出
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.