[英]NodeJS Passport Facebook OAuth
我一直在NodeJS上學習課程和觀看教程,並決定在應用程序中很好地使用它們。
對於這個項目,我需要用戶注冊並登錄才能將他們的活動存儲在數據庫中。 我使用Passport來完成這個過程,這個項目的代碼是這樣的:
/****** 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使用的教程幾乎使用相同的代碼,我更改了User模型,因為教程使用的是Mongoose,我使用的是Sequelize,但是這方面工作得很好,當我點擊注冊FB時它會注冊我或者登錄我,查詢完成工作。
但是,不起作用的是重定向。 當我使用Facebook注冊時,它會卡住並且不會加載任何東西(輪子在index.html(FB按鈕所在的位置)保持旋轉並且不加載任何東西)。 當我使用Facebook登錄時,它只在屏幕上顯示:
[對象SequelizeInstance:用戶]
在本教程中,講師使用EJS作為模板語言,但是我已經使用HTML,CSS和jQuery構建了項目前端的95%(是的,應該使用React或Angular但是時間敏感且已經在學習Node )。 我相信這是發生這種情況的原因之一,但我不是百分之百確定這里發生了什么以及為什么我得到錯誤或如何繞過。
任何幫助表示贊賞,如果需要更多信息/代碼,請告訴我。 謝謝
所以經過大量的時間調試並得到一些好的幫助后,我弄清楚是什么導致了我的問題,實際上有三個錯誤。
首先,在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之后的回調已切換參數,所以每次都給我一個錯誤,即使它沒有,所以我切換了這些並添加了一個查詢,在創建它后在DB中查找用戶能夠歸還它。
在第二個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即可維護我使用的命名約定。
希望這有助於其他人使用Sequelize和Passport。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.