NodeJS Passport Facebook OAuth



我已经上了一段时间在nodejs上观看教程,并决定在应用程序中充分利用它们。

对于此项目,我需要用户注册和登录以存储其活动在数据库中。我使用护照进行此过程,该项目的这一部分的代码是:

/****** Passport functions ******/
passport.serializeUser(function (user, done) {
    done(null, user.id);
});
passport.deserializeUser(function (id, done) {
    db.user.findOne( { where : { idUser : id } }).then(function (err, user) {
        done(err, user);
    });
});
//Facebook
passport.use(new FacebookStrategy({
    //Information stored on config/auth.js
    clientID: configAuth.facebookAuth.clientID,
    clientSecret: configAuth.facebookAuth.clientSecret,
    callbackURL: configAuth.facebookAuth.callbackURL,
    profileFields: ['id', 'emails', 'displayName', 'name', 'gender'] 
}, function (accessToken, refreshToken, profile, done) {
    //Using next tick to take advantage of async properties
    process.nextTick(function () {
        db.user.findOne( { where : { idUser : profile.id } }).then(function (err, user) {
            if(err) {
                return done(err);
            } 
            if(user) {
                return done(null, user);
            } else {
                db.user.create({
                    idUser : profile.id,
                    token : accessToken,
                    nameUser : profile.displayName,
                    email : profile.emails[0].value,
                    sex : profile.gender
                });
                return done(null);
            }
        });
    });
}));
app.use(express.static(__dirname + '/public/'));
/* FACEBOOK STRATEGY */
// Redirect the user to Facebook for authentication.  When complete,
// Facebook will redirect the user back to the application at
//     /auth/facebook/callback//
app.get('/auth/facebook', passport.authenticate('facebook', { scope : ['email']}));
/* FACEBOOK STRATEGY */
// Facebook will redirect the user to this URL after approval.  Finish the
// authentication process by attempting to obtain an access token.  If
// access was granted, the user will be logged in.  Otherwise,
// authentication has failed.
app.get('/auth/facebook/callback', 
    passport.authenticate('facebook', { successRedirect: '/app',
                                      failureRedirect: '/' }));
app.get('/', function (req, res) {
    res.render('/');
});
app.get('/app', isLoggedIn, function (req, res) {
    res.sendFile('app.html');
});
function isLoggedIn(req, res, next) {
    if(req.isAuthenticated()) {
        return next();
    } else {
        res.redirect('/');
    }
}

我在Facebook auth上使用Passport使用的教程几乎使用了相同的代码,我更改了用户模型,因为教程使用了Mongoose,并且我正在使用quelize,但是当我单击以使用FB注册时,此方面很棒记录我或记录我,查询进行工作。

但是,不起作用的是重定向。当我使用Facebook注册时,它会卡住并且不会加载任何东西(Wheel在index.html(FB按钮所在)上不断旋转,并且没有加载任何东西)。当我使用Facebook登录时,它仅在屏幕上显示:

[object quelizeInstance:用户]

在教程上,讲师使用EJS作为模板语言,但是我已经使用HTML,CSS和jQuery构建了项目前端的95%(是的已经学习节点)。我相信这是发生这种情况的原因之一,但是我不确定这里发生了什么以及为什么要遇到错误或如何解决。

如果需要更多信息/代码,请告诉我任何帮助,请告诉我。谢谢

所以经过大量时间调试,并在一些很好的帮助下,我弄清楚了是什么原因引起了我的问题,实际上有三个错误。

首先,在Facebook策略中,这就是我应该建造的:

passport.use(new FacebookStrategy({
    //Information stored on config/auth.js
    clientID: configAuth.facebookAuth.clientID,
    clientSecret: configAuth.facebookAuth.clientSecret,
    callbackURL: configAuth.facebookAuth.callbackURL,
    profileFields: ['id', 'emails', 'displayName', 'name', 'gender'] 
}, function (accessToken, refreshToken, profile, done) {
    //Using next tick to take advantage of async properties
    process.nextTick(function () {
        db.user.findOne( { where : { idUser : profile.id } }).then(function (user, err) {
            if(err) {
                return done(err);
            } 
            if(user) {
                return done(null, user);
            } else {
                //Create the user
                db.user.create({
                    idUser : profile.id,
                    token : accessToken,
                    nameUser : profile.displayName,
                    email : profile.emails[0].value,
                    sex : profile.gender
                });
                //Find the user (therefore checking if it was indeed created) and return it
                db.user.findOne( { where : { idUser : profile.id } }).then(function (user, err) {
                    if(user) {
                        return done(null, user);
                    } else {
                        return done(err);
                    }
                });
            }
        });
    });
})); 

db.user.findone之后的回调已经切换了参数,因此即使没有一个,它也会给我一个错误创建它以返回它。

在第二个Facebook路线上,这就是我的构建方式:

app.get('/auth/facebook/callback',
    passport.authenticate('facebook', { failureRedirect: '/' }),
    function(req, res) {
        // Successful authentication, redirect home.
        res.redirect('../../app.html');
    });

这使我能够继续使用HTML(我可能会重写它以稍后使用更好的视图),并且在测试时,我能够从req.user获取信息。

最后,我在Passport的Serializeuser上遇到了一个小的命名错误:

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

只是从user.id更改为user.iduser来维护我使用的命名约定。

希望这可以帮助其他人使用护照续集。

最新更新