我是Node.js和Express的新手。我試圖用Twitter完成everyauth的配置,我試圖訪問服務器端的'res.user',根據everyauth的文檔要求.findByUserId被配置。那是我遇到麻煩的地方。你如何配置findByUserId?爲everyauth配置findByUserId - node,express,mongoose,everyauth
這就是我得到的。它目前正在努力創建一個新用戶或登錄他們,我沒有正確設置.findByUserId,但讓我訪問我需要在服務器端的req.user。如果您對如何優化此代碼有其他建議,請讓我知道,因爲我只是在學習。
下面的代碼編輯...
App.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' });
});
Users.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;
});
}
怎樣的everyauth.everymodule.findUserById需要爲了找我訪問req.user?在當我登錄req.user後,我成功地登錄了默認路由,它顯示「未定義」
嗨JohnnyHK,我根據您的建議對原始文章中的代碼進行了更新。具體而言,我更改了everyauth.everymodule.findUserById函數以匹配您的示例,並更新了我的.findById訪問器我的users.js。我仍然沒有得到req.user。任何其他想法? – aressidi
默認情況下,everyauth在您的用戶對象中使用「id」主鍵字段,因此它是持續到會話並傳遞到您註冊的findUserById回調的字段。因此,如果您尚未修改默認設置,則需要更改findById的findOne調用來傳遞{'id':userId}。 – JohnnyHK