Reputation: 1700
i created this passportAuth.js
file fo authentication:
module.exports = function(passport, FacebookStrategy, config, mongoose, fbgraph){
passport.serializeUser(function(user,done){ //make the user reference available threw multiple pages
done(null,user.id);
});
passport.deserializeUser(function(id,done){
userModel.findById(id, function(err, user){
done(err, user);
})
})
passport.use(new FacebookStrategy({
clientID: config.fb.appID,
clientSecret: config.fb.appSecret,
callbackURL: config.fb.callbackURL,
profileFields: ['id', 'displayName', 'photos', 'birthday', 'events', 'profileUrl', 'emails', 'likes']
}, function(accessToken, refershToken, profile, done){
var fb = new fbgraph.Facebook(accessToken, 'v2.2');
fb.me(function(err, me) {
console.log(me);
});
fb.my.events(function(err, events) {
console.log(events);
});
fb.my.friends(function(err, result){
console.log(result);
});
}))
}
until here every thing works ok and i am able to create the fb
var with the data i want. the problem starts when i am trying to transfer this fb object to the express router inside my routes.js
file. i want to render the data stored in this object into the welcome.html
page but i cant figure out how to pass this var to the routes file and pass it to the render function.
this is how my routes file looks like:
module.exports = function(express, app, passport){
var router = express.Router();
router.get('/', function(req,res,next){
res.render('index', {title: "welcome to aDating"});
})
function securePages(req, res, next){
if(req.isAuthenticated()){
next();
} else {
res.redirect('/');
}
}
router.get('/auth/facebook', passport.authenticate('facebook', { scope: [ 'email', 'user_friends', 'public_profile', 'user_events' ] }));
router.get('/auth/facebook/callback', passport.authenticate('facebook', {
successRedirect:'/welcome', //if authentication is successful, navigate to this path
failureRedirect:'/' //else, navigate here
}))
router.get('/welcome', securePages, function(req, res, next){
res.render('welcome', {title:'Welcome to aDating', user:req.user, ----PASS fb HERE--});
})
}
any help please?
Upvotes: 0
Views: 64
Reputation: 11052
try passReqToCallback: true
, add something to the req object, and use it in a later middleware
Beware! This will clear after the request/response cycle ends! So e.g. if you set req.tmpPassport
in a request to /oauth/start
, and it redirects to facebook and back to /oauth/end
, req.tmpPassport
will be undefined again. If you need to keep track of something across redirects, use req.session
instead.
passport.use(new FacebookStrategy({
passReqToCallback: true,
clientID: config.fb.appID,
clientSecret: config.fb.appSecret,
callbackURL: config.fb.callbackURL,
profileFields: ['id', 'displayName', 'photos', 'birthday', 'events', 'profileUrl', 'emails', 'likes']
}, function(req, accessToken, refershToken, profile, done){
req.tmpPassport = {};
var fb = new fbgraph.Facebook(accessToken, 'v2.2');
fb.me(function(err, me) {
req.tmpPassport.me = me;
});
fb.my.events(function(err, events) {
req.tmpPassport.events = events;
});
fb.my.friends(function(err, result){
req.tmpPassport.results = results;
});
}))
Upvotes: 1