无法从每个用户那里获得自定义用户

时间:2023-01-12 17:55:14

I did read manuals about access to current user via everyauth. It's say, that I can read current user info from: req.user on my server, everyauth.user and user on my views, but they are undefined. But if I'm try get access from, for example, everyauth.twitter or everyauth.facebook, I'm get user info from this social networks.

我确实读过关于通过everyauth访问当前用户的手册。据说,我可以从req中读取当前的用户信息。我的服务器上的用户,每个人。用户和用户在我的视图上,但它们是未定义的。但是如果我试着从,比如说,每个人那里获取。twitter或everyauth。facebook,我从这个社交网络获取用户信息。

I'm want, when get user from database (find or create by social data) it's must save in session variable, like currentUser, and i can get it in helpers and in other databare requests.

我想,当从数据库(查找或创建社交数据)获取用户时,它必须保存在会话变量中,比如currentUser,我可以在helper和其他databare请求中获取它。

My app.js code:

我app.js代码:

var express     = require('express')
  , everyauth   = require('everyauth')
  , Promise     = everyauth.Promise
  , util        = require('util')
  , mongoose    = require('mongoose')
  , routes      = require('./routes')
  , _           = require('underscore')

mongoose.connect('mongodb://127.0.0.1/base');
var Schema = mongoose.Schema
  , ObjectId = Schema.ObjectId;

// Everyauth settings above that app.configure

everyauth.twitter
  .consumerKey('secretKey')
  .consumerSecret('secret')
  .findOrCreateUser(function (session, accessToken, accessTokenSecret, twitterUserData){
      var promise = this.Promise();
      User.findOrCreateByUidAndNetwork(twitterUserData.id, 'twitter', twitterUserData, promise);
      return promise;
  })
  .redirectPath('/')
everyauth.facebook
    .appId("secretId")
    .appSecret("secret")
    .findOrCreateUser( function (session, accessToken, accessTokenExtra, fbUserMetadata) {
      var promise = this.Promise();
      User.findOrCreateByUidAndNetwork(fbUserMetadata.id, 'facebook', fbUserMetadata, 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.cookieParser());
  app.use(express.session({secret:'blablabla'}));
  app.use(everyauth.middleware()); // Yes, i'm use it
  app.use(express.methodOverride());
  app.use(app.router); // And it
  app.use(express['static'](__dirname + '/public'));
});

everyauth.helpExpress(app); // And this above that routes

app.dynamicHelpers({
    currentUser: function (req, res){
        return req.user; //it's empty!
    }
})

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

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

// Routes

app.get('/', routes.index);
require("./controllers/user");

app.listen(80);

And user Scheme:

和用户方案:

var Schema = mongoose.Schema
  , ObjectId = Schema.ObjectId;

var UserSchema = new Schema({
    "uid":{type:String},    
    "name":{type:String},   
    "network":{type:String},
    "profile":{}        
});


mongoose.model('User', UserSchema);

var User = mongoose.model('User');

And user find or create function:

和用户查找或创建功能:

this.findOrCreateByUidAndNetwork = function(uid, network, profile, promise) {
    User.find({uid: uid, network: network}, function(err, users) {
        if(err) throw err;
        if(users.length > 0) {
            promise.fulfill(users[0]);// <-- what i want:)
        } else {
            var user = new User();
            user.network = network;
            user.uid = uid;
            user.profile = profile;
            user.name = profile.first_name || profile.name;
            user.save(function(err) {
                if (err) throw err;
                promise.fulfill(user);
            });
        }
    });
};

Thanks, for watching my question. Best regards, Asci

谢谢大家收看我的提问。最好的问候,子囊

1 个解决方案

#1


3  

Possibly a little late, but for anyone who needs an answer to this -

可能有点晚了,但是对于任何需要答案的人来说-

On Everyauth's Github page it specifies that "To access the user, configure everyauth.everymodule.findUserById". So for example (again quoting bnoguchi) -

在Everyauth的Github页面上,它指定“要访问用户,请配置Everyauth . everymodule.finduserbyid”。举个例子(再次引用了bnoguchi) -

everyauth.everymodule
    .findUserById( function (id, callback) {
        yourApi.fetchUserById(id, function (err, user) {
        if (err) return callback(err);
        callback(null, user);
     });
     // or more succinctly, if your api sends a user to the callback with function signature function (err, user):
     // yourApi.fetchUserById(id, callback);
});

#1


3  

Possibly a little late, but for anyone who needs an answer to this -

可能有点晚了,但是对于任何需要答案的人来说-

On Everyauth's Github page it specifies that "To access the user, configure everyauth.everymodule.findUserById". So for example (again quoting bnoguchi) -

在Everyauth的Github页面上,它指定“要访问用户,请配置Everyauth . everymodule.finduserbyid”。举个例子(再次引用了bnoguchi) -

everyauth.everymodule
    .findUserById( function (id, callback) {
        yourApi.fetchUserById(id, function (err, user) {
        if (err) return callback(err);
        callback(null, user);
     });
     // or more succinctly, if your api sends a user to the callback with function signature function (err, user):
     // yourApi.fetchUserById(id, callback);
});