我正在尝试按照passport-facebook GitHub上的示例设置Facebook身份验证,但我没有将会话存储起来。一旦我登录Facebook并被重定向回" /",就没有证据表明我已在我的应用程序中登录。因为当我的登录确实在Facebook上成功时,它指示我回到successRedirect路线,似乎我缺少保存会话所需的代码。
在我的app.js文件中,我有以下内容。
var express = require("express"),
bodyParser = require("body-parser"),
passport = require("./auth/local"),
FacebookStrategy = require("passport-facebook"),
LinkedInStrategy = require("passport-linkedin"),
GoogleStrategy = require("passport-google"),
cookieParser = require('cookie-parser'),
session = require("express-session"),
fbStrategy = require("./auth/facebook");
var app = express();
app.use(cookieParser());
app.use(session({
resave: true,
saveUninitialized: true
}));
app.use(express.static(__dirname + "/public"));
app.set("view engine","ejs");
app.use(passport.initialize());
app.use(passport.session());
app.get('./auth/facebook',
fbStrategy.authenticate('facebook'
));
app.get('./auth/facebook/callback',
fbStrategy.authenticate('facebook', {
successRedirect: "/",
failureRedirect: '/login' })
);
在路线/auth/facebook.js中我有以下代码
var passport = require('passport'),
FacebookStrategy = require('passport-facebook'),
authHelpers = require("./_helpers");
var init = require('./passport');
init();
passport.use(new FacebookStrategy({
clientID: [...],
clientSecret: [...],
callbackURL: "callbackurl",
//for profFields look here for definitions... https://developers.facebook.com/docs/graph-api/reference/v2.5/user
profileFields: ['id','first_name','last_name','gender','political','verified','currency','birthday']
},
function(accessToken, refreshToken, profile, done) {
function(accessToken, refreshToken, profile, cb) {
function(accessToken, refreshToken, profile, done) {
knex('user').select('email','user_id').where({facebookID:profile.id})
.then((user) => {
if (user.length==0) {
knex('user').insert({
email: profile.emails[0].value,
facebookID:profile.id,
firstname: profile.name.givenName,
lastname: profile.name.familyName
})
return done(null,user);
} else {
return done(null, user);
}
})
.catch((err) => {console.log("problem near line 24 in auth/facebook.js... "+err); return done(err); });
}
}));
module.exports = passport;
我的数据库配置在其他任何地方都运行正常,所以我没有在上面的代码段中包含配置代码/文件。