Passport-Facebook不搜索我的模型

时间:2014-04-12 09:29:31

标签: sails.js passport.js

我正在尝试使用visajs与facebook和sails js.in中间件,这是我的实现

var verifyHandler = function (token, tokenSecret, profile, done) {
process.nextTick(function () {

    //check if the user exist or not
    var uid=profile.id;

    User.findOne({uid:profile.id}).done(function(err,user){
       if(user)
       {
        console.log('found');
        //done(null,user);
       }
       else
       {
        console.log('user does not exists');
       }
     });
   };

奇怪的是,它无法搜索模型。我在哪里做错了?

1 个答案:

答案 0 :(得分:0)

我终于弄清楚了,我希望它能帮助任何有MySQL问题的人。 1.在您的用户模型中,uid数据类型应该是STRING而不是INTEGER。请确保您对数据库表结构的相同更改对VARCHAR有效。 2.更改middleware.js verifyHandler(省略parseInt)

var passport = require('passport')
, GitHubStrategy = require('passport-github').Strategy
, FacebookStrategy = require('passport-facebook').Strategy
, GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;

var verifyHandler = function (token, tokenSecret, profile, done) {
process.nextTick(function () {

    User.findOne({uid: profile.id}).done(function (err, user) {

            if (user) {
               return done(null, user);

            } else {



                var data = {
                    provider: profile.provider,
                    uid: profile.id,
                    name: profile.displayName
                };

                if(profile.emails && profile.emails[0] && profile.emails[0].value) {
                    data.email = profile.emails[0].value;
                }
                if(profile.name && profile.name.givenName) {
                    data.firstname = profile.name.givenName;
                }
                if(profile.name && profile.name.familyName) {
                    data.lastname = profile.name.familyName;
                }

                User.create(data).done(function (err, user) {
                        return done(err, user);
                    });

          } 

        });               


});
};

passport.serializeUser(function (user, done) {
done(null, user.uid);
});

passport.deserializeUser(function (uid, done) {
User.findOne({uid:uid}).done(function (err, user) {
    done(err, user)
});
});
module.exports = {

// Init custom express middleware
express: {
    customMiddleware: function (app) {

        passport.use(new FacebookStrategy({
                clientID: "****",
                clientSecret: "********",
                callbackURL: "http://localhost:1337/auth/facebook/callback"
            },
            verifyHandler
        ));


        app.use(passport.initialize());
        app.use(passport.session());

    }
}

};

希望这可以帮助别人。欢呼!