0

我几乎可以通过 Facebook 进行 Passport 身份验证,但是我的模型中的 FacebookStrategy 回调从未被调用,因此无法获取个人资料信息。我一定错过了一些东西,但没有运气弄清楚。

我的 App.js

var express     = require('express');
var http        = require('http');
var path        = require('path');
var mongoose    = require('mongoose');
var fs          = require('fs');
var io          = require('socket.io');
var config      = require("./server/config");
var passport    = require("passport");

//init Express
var app = express();


var allowCrossDomain = function(req, res, next) {
    res.header('Access-Control-Allow-Origin', "*");
    res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.header('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
    res.header('Access-Control-Allow-Credentials', true);

    next();
}



//client code could be found here
var clientDir = path.join(__dirname, 'www');
//set up Node app configurations
app.configure(function(){
    app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.cookieParser());
    app.use(express.bodyParser());
    app.use(express.session({
        secret: 'adasdasdasdasdasdasdasdasdasdasd'
    }));
    app.use(passport.initialize());
    app.use(passport.session());
    app.use(express.methodOverride());
    app.use(allowCrossDomain);
    app.use(app.router);
    app.use(express.static(clientDir));
});


// Mongo startup
var mongoUri    = config.development.mongoUrl;

var db = mongoose.connect(mongoUri);
mongoose.set('debug', true);
mongoose.connection.on("open", function() {
    console.log("Mongo Open on: " + mongoUri);
    init();
});
mongoose.connection.on("error", function() {
    console.log("Mongo ERROR");
});



var init = function(){

    var models = {
        User:   require('./server/models/User')(app, mongoose, config, passport)
    };

    //load up routes
    require('./server/routes/FacebookAuth')(app, models, config, passport);

}


// Create an http server
app.server = http.createServer(app);

//go to 'client' index page
app.get('/', function(req, res){
    res.sendfile(path.join(clientDir, 'index.html'));
});

var portNum = process.env.PORT || 3004;
app.server.listen(portNum, function(){
    var addr = app.server.address();
    console.log('   app listening on http://' + addr.address + ':' + addr.port);
});

我的路线

module.exports = function (app, models, config, passport) {

app.get('/auth/facebook', passport.authenticate('facebook', {
    scope: ['read_stream', 'publish_actions']
}));
app.get('/auth/facebook/callback',
    passport.authenticate('facebook', {
        successRedirect: '/#/maker/123456',
        failureRedirect: '/#/login'
    }));

}

我的模特

module.exports = function (app, mongoose, config, passport) {

var FacebookStrategy    = require('passport-facebook').Strategy;

var UserSchema = new mongoose.Schema({

    fbId: String,
    name: String,
    email: {
        type:String,
        lowercase: true
    }

});

console.log("+++++++++++++ User Model");

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

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

passport.deserializeUser(function(obj, done) {
    done(null, obj);
});

passport.use(new FacebookStrategy({
        clientID : config.development.fb.appId,
        clientSecret: config.development.fb.appSecret,
        callbackURL:  config.development.fb.url + '/#/maker/12345'
    },
    function(acccessToken, refreshToken, profile, done){

        //THIS IS NEVER BEING CALLED!
        console.log("################################");
        process.nextTick(function(){
            return done(null, profile);
            console.log("################################");
        })
    }
));

/*****************
 *    Public API
 *****************/
return {

    User: User,

    /***GET***/

    //Get All Items

    //Get "one" item by Id
    findById: function (id, items, callback) {
        User.findById(id, items, function (err, doc) {
            callback(doc);
        });
    },

    //Get All Items
    findAll: function (callback) {
        User.find({}, function (err, doc) {
            callback(doc);
        });
    }

}

}
4

1 回答 1

0

我误解了谁的策略有效

这就是我所拥有的

passport.use(new FacebookStrategy({
        clientID : config.development.fb.appId,
        clientSecret: config.development.fb.appSecret,
        callbackURL:  config.development.fb.url + '/#/maker/12345'

它实际上应该有一个回调 URL

/auth/facebook/callback

然后重定向发生在回调中

于 2014-01-13T19:57:58.903 回答