Couldn’t get everyauth’s req.user inside router functions

I am a newbie of node.js
and everyauth. While I just playing around everyauth I have realized that everyauth.user
works in jade views but I couldnt get req.user
work inside page callbacks.

/**
 * 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);

Here is the problematic part

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' });
});

It seems I am missing some point. The weird part is req.loggedIn
works as expected but somehow everyauth don’t populate user data.

Problem courtesy of: Burak Bayer

Solution

I’ve been having issues with everyauth req.user with the latest versions. What I do is I use “req.session…”. So If I want to get the user while under the facebook oauth, I’d use req.session.auth.facebook.user.id, and this gives me the users Id.

console.log(req.session) and from there extract whatever information you want to use.

Solution courtesy of: guiomie

Discussion

To access the ‘user’ by req.user you need to configure everyauth.everymodule.findUserById like below

//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

Discussion courtesy of: slv007

This recipe can be found in it’s original form on Stack Over Flow
.

稿源:Node.js Recipes (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » Couldn’t get everyauth’s req.user inside router functions

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录