如何在我的应用程序中使用Facebook登录

时间:2017-11-14 09:47:42

标签: node.js facebook

api / users.js

var fb = {
    clientID: 'xxxxxxxxxxxxxxxx',
    clientSecret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    callbackURL: 'http://localhost:4268/auth/facebook/callback'
};

var fbCallback = function(accessToken, refreshToken, profile, cb) {
    console.log(accessToken, refreshToken, profile);
};

passport.use(new FacebookStrategy(fb, fbCallback));
    

router.get('/', passport.authenticate('facebook', {scope:"email"}));
router.get('/auth/facebook/callback',function(req, res) {
    res.send("logged in");
});
我已将此代码放在名为api的{​​{1}}文件夹中,当我登录facebook时,我正在使用users.js。登录后,我希望消息“登录”,但事实并非如此。回调url中是否有任何错误?

1 个答案:

答案 0 :(得分:0)

更改

    var fb = {
    clientID: 'xxxxxxxxxxxxxxxx',
    clientSecret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    callbackURL: 'http://localhost:4268/auth/facebook/callback'
};

    var fbCallback = function(accessToken, refreshToken, profile, cb) {
    console.log(accessToken, refreshToken, profile);
};

    var fb = {
    clientID: 'xxxxxxxxxxxxxxxx',
    clientSecret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    callbackURL: 'http://localhost:4268/auth/facebook/callback',
    profileFields:['id','displayName','emails']
};

    var fbCallBack = function(accessToken, refreshToken, profile, done) {
        console.log(profile);
        var me = new user({
            email:profile.emails[0].value,
            name:profile.displayName
        });

        /* save if new */
        user.findOne({email:me.email}, function(err, u) {
            if(!u) {
                me.save(function(err, me) {
                    if(err) return done(err);
                    done(null,me);
                });
            } else {
                console.log(u);
                done(null, u);
            }
        });