gpt4 book ai didi

node.js - 无法在路由器功能内获取 everyauth 的 req.user

转载 作者:太空宇宙 更新时间:2023-11-03 22:48:25 25 4
gpt4 key购买 nike

我是 node.js 和 everyauth 的新手。当我只是玩 everyauth 时,我意识到 everyauth.user 在 jade View 中工作,但我无法让 req.user 在页面回调中工作。

/**
* Module dependencies.
*/


var express = require('express')
, routes = require('./routes')
, everyauth = require('everyauth')
, Promise = everyauth.Promise
, util = require('util')
, models = require('./models');



//Everyauth setup

everyauth.twitter
.consumerKey('XXX')
.consumerSecret('XXX')
.findOrCreateUser(function(session, accessToken, accessTokenSecret, twitterData){
var promise = new Promise();
models.findOrCreateTwitterUser(twitterData,promise);
return promise;
})
.redirectPath('/');

var app = module.exports = express.createServer();
// Configuration

app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.static(__dirname + '/public'));
app.use(express.cookieParser());
app.use(express.session({secret:"Iam james bond"}));
app.use(express.methodOverride());
//app.use(app.router);
app.use(everyauth.middleware());
});

app.configure('development', function(){
everyauth.debug = true;
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
app.use(express.errorHandler());
});

// Routes

app.get('/', function(req, res){
if(req.loggedIn){
console.log(req.user); //<--This is always undefined
}
console.log("index");
res.render('index', { title: 'Welcome to Chat app' });
});

//Helpers
everyauth.helpExpress(app);

app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

这是有问题的部分

app.get('/', function(req, res){
if(req.loggedIn){
console.log(req.user); //<--This is always undefined
}
console.log("index");
res.render('index', { title: 'Welcome to Chat app' });
});

我似乎遗漏了一些要点。奇怪的部分是 req.loggedIn 按预期工作,但不知何故 everyauth 不填充用户数据。

最佳答案

要通过 req.user 访问“用户”,您需要如下配置 everyauth.everymodule.findUserById

//Everyauth setup
....
....
everyauth.everymodule.findUserById( function (userId, callback) {
var db = app.set('db');
var User = db.main.model('User');
User.findById(userId, callback);
// callback has the signature, function (err, user) {...}
});
....
....
var app = module.exports = express.createServer();
//Configuration

关于node.js - 无法在路由器功能内获取 everyauth 的 req.user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9087902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com