I am trying to connect to my user via Facebook using PassportJS and transfer the user data to Angular. On the server side, everything looks fine with the following code for Facebook callback in user controller:
exports.facebookCallback = function() { return function(req, res, next) { passport.authenticate('facebook', function(err, user, email) { if (err || !user) { return res.redirect('/auth'); } req.login(user, function(err) { if (err) { return res.redirect('/auth'); } return res.redirect('/'); }); })(req, res, next); }; };
From what I understand from PassportJS docs, calling req.login should put the user data in the session.
My server side routes look like this:
app.get('/auth', usersCtrl.auth); app.get('/auth/signout', usersCtrl.logout); app.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email', 'user_hometown'] })); app.get('/auth/facebook/callback', usersCtrl.facebookCallback());
Express and passport configuration includes:
app.use(express.cookieParser()); app.use(express.session({secret: '1234567890QWERTY'})); app.use(express.bodyParser()); app.use(passport.initialize()); app.use(passport.session());
Now on the angular side, I am trying to get user data from a session in a service defined as follows:
module.exports = require('angular') .module('HomeModule', []) .controller('HomeCtrl', function ($scope) { //home controller code ors here }).controller('NavbarCtrl', ['$scope', 'Authentication', function ($scope, Authentication) { $scope.authentication = Authentication; //rest of the navbar controller goes here }]).factory('Authentication', [ function() { var _this = this; _this._data = { user: window.user }; return _this._data; } ]);
At ease, user data is not available in the window.user window on the angular side. Any ideas what I'm doing wrong here?