deserializeUser function never called with Google Strategy in passportJS - node.js

I'm trying to setup google auth with PassportJS, but my deserializeUser function is never called.
This is my passport.js config:
...
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(sessionUser, done) {
User.findById(sessionUser, function(err, user) {
done(err, user);
});
});
passport.use('google', new GoogleStrategy({
clientID : process.env.GOOGLE_CLIENTID,
clientSecret : process.env.GOOGLE_CLIENTSECRET,
callbackURL : process.env.GOOGLE_CALLBACKURL,
passReqToCallback: true,
},
function(req, token, refreshToken, profile, done) {
process.nextTick(function() {
// console.log(profile);
var values = {
where: { google_id: profile.id },
defaults: {google_id: profile.id, name: profile.displayName}
};
User.findOrCreate(values)
.spread(function(user, created) {
return done(null,user);
});
});
}
));
And these are my routes:
app.get('/auth/google',
passport.authenticate('google', { scope : ['profile', 'email'] }));
// the callback after google has authenticated the user
app.get('/auth/google/callback',
passport.authenticate('google', {
successRedirect : '/portfolio/crypto',
failureRedirect : '/'
}));
And since deserializeUser is never called, req.user is never defined. How can I fix this?

Related

Facebook strategy with sapper and polka

I need to make a connection with polka (not Express) and using a passport-facebook strategy. On my serverjs:
var app = polka();
passport.serializeUser(function(user, cb){
return cb(null, profile);
});
passport.deserializeUser(function(obj, cb){
return cb(null, obj);
});
passport.use(new Strategy({
clientID: *****,
clientSecret: '******',
callbackURL: 'http://localhost:3000/login/facebook/callback',
//campo opzionale, specifico gli oggetti del profilo che voglio tornato
profile: ['id', 'displayName']
},
function(accessToken, refreshToken, profile, done) {
//Check the DB to find a User with the profile.id
User.findOne({ uid: profile.id }, function(err, user) {
if(err) {
console.log(err); // handle errors!
}
if (user) {
console.log("find user!", user);
done(null, user); //Login if User already exists
} else { //else create a new User
user = new User({
uid: profile.id, //pass in the id and displayName params from Facebook
name: profile.displayName
});
user.save(function(err) { //Save User if there are no errors else redirect to login.
if(err) {
console.log(err); // handle errors!
} else {
console.log("saving user ...");
done(null, user);
console.log("user", user);
}
});
}
});
}
));
app
//va sempre messo prima di passport.session
.use(require('express-session')({ secret: 'keyboard cat', resave: true, saveUninitialized: true }))
.use(passport.initialize())
.use(passport.session())
//definisco le rotte
//rotta di login
.get('/login/facebook',
passport.authenticate('facebook')
)
//callback
.get('/login/facebook/callback',
//da confermare se è supportato il fialureRedirect
//passport.authenticate('facebook', {failureRedirect: '/'}),
passport.authenticate('facebook', {failureRedirect: '/'}),
function(req, res){
res.writeHead(302, {
'Location': '/admin/menu',
'content-type': 'document'
});
res.end();
}
)
.use(
compression({ threshold: 0 }),
sirv('static', { dev }),
formData.parse(options),
formData.format(),
//formData.stream(),
formData.union(),
json(),
sapper.middleware()
)
When i make the login the console shows also that i'm already logged in (it created also a object on the mongodbclient and it's also finded), but the problem is that redirect me on some url with
http://localhost:3000/login/facebook/callback?code=AQBBJIV61HxahPCtWBgMR23OMpU8MXfxDcD3BeRDTUw2rK5FvTnXq5n...
with a message "profile is not defined"
and on the console i have this on pending
On serializeUser you return profile that is never declared you have to use:
passport.serializeUser(function(user, cb){
return cb(null, user);
});

Facebook authentication using Passport in Node.js

I am implementing a Facebook authentication using Passport in node.js. I can get the users profile from Facebook successfully. The problem is, the code should redirect the user to the successRedirect: /profile but instead it always goes to /fbFailure. No matter what I tried, it always goes to the url : somesite.com:6161/fbFailure#=
Passport middleware code:
var FacebookStrategy = require('passport-facebook').Strategy;
var models = require('../models');
passport.use('facebook', new FacebookStrategy(
{
clientID: '1735552370024305', clientSecret: 'a004f1aee925b615022d7573984d5c26', callbackURL: "http://www.somesite.com:6161/facebookSuccess", profileFields: ['id', 'displayName', 'photos', 'emails'],
},
function(access_token, refreshToken, profile, done) {
console.log('profile', profile);
models.Member.findOne({ '__id' : profile.id }, function(err, user) {
if (err)
return done(err);
if(user){
return done(null, user);
}
done(null, user);
})
}
));
My Routes:
app.get('/facebook', passport.authenticate('facebook', { scope : 'email' }));
app.get('/facebookSuccess', passport.authenticate('facebook', {
successRedirect : '/profile',
failureRedirect : '/fbFailure'
}));
app.get('/profile', function(req, res, next) {
res.send('Successfully authenticated');
});
app.get('/fbFailure', function(req, res, next) {
res.send('Failed to authenticate');
});
Console Output:
json:
{ id: '10154130288931542',
name: ‘Tom Verra’,
picture: { data: [Object] },
email: ‘tom.vera#mysite.com' } }
GET /facebookSuccess?code=AQBzdIfKPQ..............YU6TvYuf......
Any help would be highly appreciated. Thanks in advance.
You could try redirecting using res.redirect('/profile');, just the way it is done in the passport-facebook readme on GitHub (https://github.com/jaredhanson/passport-facebook#authenticate-requests):
app.get('/facebookSuccess',
passport.authenticate('facebook', { failureRedirect: '/fbFailure' }),
function(req, res) {
res.redirect('/profile');
});

Save accessToken to local storage using passport-facebook

I'm new to MEAN Stack, I'm having trouble saving passport-facebook accessToken to localStorage. How do I do this? Below is my setup.
passport.use(new FacebookStrategy({
clientID: passport_config.facebook.clientID,
clientSecret: passport_config.facebook.clientSecret,
callbackURL: passport_config.facebook.callbackURL
},
function(accessToken, refreshToken, profile, done) {
FBAccount.findOne({fbId : profile.id}, function(err, oldUser){
if(oldUser){
done(null,oldUser);
}else{
var newUser = new FBAccount({
fbId : profile.id ,
name : profile.displayName
}).save(function(err,newUser){
if(err) throw err;
console.log(newUser);
done(null, newUser);
});
}
});
}
));
Try this
var localStorage = require('localStorage')
localStorage.setItem('accessToken', accessToken);
FBAccount.findOne({ ....
You can also add token in the cookies by some middleware like
passport.use(new FacebookStrategy({
clientID: '566950043453498',
clientSecret: '555022a61da40afc8ead59c6c26306ed',
callbackURL: 'http://www.localhost:3000/auth/facebook/callback'
}, function(accessToken, refreshToken, profile, done) {
console.log("hello " + profile.displayName);
done(null);
}
));
//Authentication
app.get('/auth/facebook', passport.authenticate('facebook'));
router.get('/auth/facebook/callback', passport.authenticate('facebook', {
failureRedirect: '/login?failedSocial=facebook'
}), auth.authCallback);
and in auth service
exports.authCallback = function (req, res) {
res.res.cookie('token', JSON.stringify(req.user.token));
res.redirect('/');
}

Error with Facebook redirect URL on login/register using passport on a node.js (MEAN stack) application

I am implementing the Facebook login and register functions to my application, and can reach facebook for the user to enter their information, but can't make the callback work. I have input the code below into passport.js (obviously x'ing out our information):
passport.use(new FacebookStrategy({
clientID: "xxxxxxxxxxxxxx",
clientSecret: "xxxxxxxxxxxxxxxxxxx",
callbackURL: "https://xxxxxxxxxxxxxxxx/auth/facebook/callback/",
enableProof: false,
profileFields: ['id', 'displayName', 'photos']
},
function (accessToken, refreshToken, profile, done) {
process.nextTick(function () {
User.findOne({facebookId: profile.id}, function (err, user) {
if (err) {
return done(err);
}
if (user) {
return done(null, user);
} else {
var data = {
facebookId: profile.id,
f_name: profile.first_name,
l_name: profile.last_name,
username: profile.email
};
if (profile.emails && profile.emails[0] && profile.emails[0].value) {
data.username = profile.emails[0].value;
}
User.create(data, function (err, user) {
return done(err, user);
});
}
});
});
}));
passport.serializeUser(function(user, callback) {
callback(null, user._id);
});
passport.deserializeUser(function(id, done) {
User.findById({
_id: id
}, function(err, user) {
callback(err, user);
});
});
passport.use(new LocalStrategy(
function (username, password, done) {
console.log(username);
console.log(password);
User.findOne({ 'username': username }, function (err, user) {
// if (err) { return done(err); }
if (!user) {
return done(null, false, { message: 'Incorrect email.' });
}
if (!user.validPassword(password)) {
return done(null, false, { message: 'Incorrect password.' });
}
return done(null, user);
});
}
));
The following callback URL has been included in index.js:
router.get('/auth/facebook', passport.authenticate('facebook', {
scope: 'email' }));
router.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect : '/user/#/home',
failureRedirect : '/'
}));
I am able to make the application direct to the Facebook login page onClick, but once the user enters their email and password into facebook, the application is unable to reload on redirect and get the person to our homepage. The redirect begins to happen, but hits an error once it tries to load our application. Could we be utilizing one of the redirect fields incorrectly? As a side note, what should we set as a valid OAuth Redirect URI on the facebook developer application page?

How to differentiate Login from Sign up when using PassportJS for Facebook

I am trying to set up goals on Google Analytics to track Sign Ups, so I set up a 'thank you ' page as my url goal. It works well when my users sign up with their email address but not when they use facebook to sign up/login. When they login, they are redirected to the thank you page as there is only one url callback when setting up Facebook using Passport JS and Node.
Here is my code:
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(id, done) {
UserActivity.findOne(id,'uid ref', function (err, user) {
done(err, user);
});
});
passport.use(new FacebookStrategy({
clientID: 'XXXXXXXXXXXX',
clientSecret: 'XXXXXXXXXXXXXXXX',
callbackURL: "https://www.xxxxxxx.com/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, done) {
//console.log(profile);
User.findOne({ uid: profile.id }, function(err, uInfo) {
if(err) console.log('Error: '+err);
else{
//User exists: we are done
if(uInfo){
done(err, uInfo);
}
else{
//User doesn't exist: we create a new one
var newUser = new User ({
uid: profile.id,
email:profile.emails[0].value,
ref: 'Facebook'
});
// Saving it to the database.
newUser.save(function (err,uInfo) {
if (err) console.log ('Error on save!');
done(err, uInfo);
});
}
}
})
}
));
app.get('/auth/facebook', passport.authenticate('facebook',{ scope: 'email' }));
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { successRedirect: '/thankyou',
failureRedirect: '/login' }));
If the user exists, I would like to redirect to their dashboard ('/dashboard) and if they are new users, I need to redirect them to /thankyou.
Any idea how to achieve this?
Thanks a lot!
Nevermind, found the answer. Here is the updated code below. Pay attention to the use of passReqToCallback and req.session.newu
passport.use(new FacebookStrategy(
{
clientID: 'XXX',
clientSecret: 'XXX',
callbackURL: "https://www.XXX.co/auth/facebook/callback",
passReqToCallback: true
},
function(req, accessToken, refreshToken, profile, done) {
//console.log(profile);
User.findOne({ uid: profile.id }, function(err, uInfo) {
if(err) console.log('Error: '+err);
else{
if(uInfo){
done(err, uInfo);
}
else{
var newUser = new User ({
uid: profile.id,
email:profile.emails[0].value,
ref: 'Facebook'
});
// Saving it to the database.
newUser.save(function (err,uInfo) {
if (err) console.log ('Error on save!');
req.session.newu=true;
done(err, uInfo);
});
}
}
})
}
));
app.get('/auth/facebook', passport.authenticate('facebook',{ scope: 'email' }));
app.get('/auth/facebook/callback',function(req, res, next) {
passport.authenticate('facebook', function(err, user, info) {
if (err) { return next(err); }
if (!user) { return res.redirect('/login'); }
req.logIn(user, function(err) {
if (err) { return next(err); }
var redLink = '/dashboard';
if(req.session.newu)redLink = '/dashboard?newu'
return res.redirect(redLink);
});
})(req, res, next);
});
An existing user will be redirected to /dashboard and a new user will be redirected to /dashboard?newu
Google Analytics doesn't need 2 different urls, it just needs a query string. When I set up the url goal, I selected url start with /dashboard?newu.
Hope this helps
The question is a bit old but it might still help someone like me. The OP's answer works but it means you have to take care of log-in user and session, etc. In case you still want to leave those work to PassportJS, use req.session.returnTo in strategy's callback with successReturnToOrRedirect option in passport.authenticate() would work.

Resources