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 に答える