我对 Node.js 和 Express 比较陌生。我正在尝试使用 Twitter 完成everyauth 的配置,并且我正在尝试访问服务器端的“res.user”,根据everyauth 的文档要求配置.findByUserId。这就是我遇到麻烦的地方。你如何配置 findByUserId?
这就是我得到的。它目前正在创建一个新用户或让他们登录,我还没有正确设置 .findByUserId 但让我可以访问我在服务器端需要的 req.user 。如果您对如何优化此代码有其他建议,也请告诉我,因为我只是在学习。
代码编辑如下...
应用程序.js
var express = require('express')
, everyauth = require('everyauth')
, util = require('util')
, Promise = everyauth.Promise
, users = require('./lib/users')
, config = require('./config.js')
, twitter = require('./lib/twitter')
, games = require('./lib/games')
everyauth.twitter
.consumerKey(config.twitter.consumerKey)
.consumerSecret(config.twitter.consumerSecret)
.findOrCreateUser( function (session, accessToken, accessTokenSecret, twitterUserMetadata) {
var promise = this.Promise();
users.findOrCreateUserbyTwitterData(twitterUserMetadata, accessToken, accessTokenSecret, promise)
return promise;
})
.redirectPath('/');
everyauth.everymodule.findUserById(function (userId, callback) {
users.findById(userId, callback);
});
var app = module.exports = express.createServer();
everyauth.helpExpress(app);
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({secret: "FxT10477A93d54HJx5"}));
app.use(everyauth.middleware());
app.set('view engine', 'jade');
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', function(req, res){
console.log(req.user);
res.render('index', { title: 'Zeitheist 2' });
});
用户.js
var twitter = require(__dirname + "/twitter");
var mongoose = require('mongoose');
var Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
var userSchema = new Schema({
user : ObjectId
, id : Number
, twitterId : Number
, twitterName : String
, profileImageUrl : String
, name : String
, dateCreated : Date
, session : String
, accessToken : String
, accessTokenSecret : String
});
var conn = mongoose.createConnection('mongodb://localhost/zeitheist2_test');
var userModel = conn.model('User', userSchema);
this.findOrCreateUserbyTwitterData = function(twitterData, accessToken, accessTokenSecret, promise) {
userModel.find({'twitterId' : twitterData.id}, function(err, users) {
if(err) throw err;
if(users.length > 0) {
promise.fulfill(users[0]);
} else {
var user = new userModel();
user.twitterId = twitterData.id
user.twitterName = twitterData.screen_name
user.profileImageUrl = twitterData.profile_image_url
user.name = twitterData.name
user.accessToken = accessToken
user.accessTokenSecret = accessTokenSecret
user.save(function(err) {
if (err) throw err;
twitter.follow(accessToken, accessTokenSecret);
promise.fulfill(user);
});
}
});
}
this.findById = function(userId, callback) {
userModel.findOne({'_id' : userId}, function(err, user) {
if(err) throw err;
if(callback)
callback(user);
else
return user;
});
}
为了让我访问 req.user,everyauth.everymodule.findUserById 需要如何查看?在我成功登录后登录 req.user 时的默认路由中,它显示“未定义”