Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default function setup(UserService, facebookConfig) {
passport.use(new FacebookTokenStrategy({
clientID: facebookConfig.clientID,
clientSecret: facebookConfig.clientSecret
}, (accessToken, refreshToken, profile, done) => {
const mainPublicEmail = profile.emails[0].value;
if (!mainPublicEmail) {
// Some times it happens
// https://developers.facebook.com/bugs/298946933534016/?comment_id=620710881344692
// https://developers.facebook.com/docs/graph-api/reference/user
done(new Error('Facebook Auth: Not found e-mail in profile'));
return;
}
UserService.findOrCreateUser(
{
email: mainPublicEmail
.exec(function(err, user) {
if (err) {
return done(err, false);
}
if (user) {
done(null, user);
} else {
done(null, false);
}
});
})
);
// Facebook
passport.use(
new FacebookTokenStrategy(
{
clientID: config.facebook.clientID,
clientSecret: config.facebook.clientSecret,
profileFields: ['id', 'first_name', 'last_name', 'email', 'picture']
},
function(accessToken, refreshToken, profile, done) {
// asynchronous
process.nextTick(function() {
profile = profile['_json'];
logger.info('========profile=--=======', profile);
// find the user in the database based on their facebook id
User.findOne({ email: profile.email }, function(err, user) {
// if there is an error, stop everything and return that
// ie an error connecting to the database
if (err) return done(err);
require("../model/user").findOne({token : token}, function(err, user) {
if(err) {
return done(err);
}
if(user) {
return done(null, user);
} else {
return done(null, false, {message : "invalid_token"});
}
});
})
);
passport.use(
new FBTokenStrategy({
clientID : app_config.FB_CLIENT_ID
, clientSecret : app_config.FB_CLIENT_SECRET
}
, function(accessToken, refreshToken, profile, done) {
var User = require("../model/user");
//console.log(profile);
User.findOne({"facebook.id" : profile.id}, function(err, user) {
if(err) {
return done(err);
}
if(user) {
user.token = accessToken;
} else {
update: {
email,
name,
picture,
},
});
done(null, user);
} catch (e) {
done(e);
}
},
),
);
passport.use(
new FacebookTokenStrategy(
{
clientID: process.env.FACEBOOK_APP_ID,
clientSecret: process.env.FACEBOOK_APP_SECRET,
},
async (accessToken, refreshToken, profile, done) => {
const {
id, displayName, emails, photos,
} = profile;
try {
const user = await prisma.upsertUser({
where: {
id: `F-${id}`,
},
create: {
id: `F-${id}`,
email: emails[0].value,
const match = await user.validatePassword(password)
if (!match) {
return done(false)
}
done(user)
} catch (err) {
done(err)
}
} catch (err) {
return done(err)
}
}))
passport.use('facebook-token', new FacebookTokenStrategy(config.facebook, async (accessToken, refreshToken, profile, done) => {
if (!accessToken || !profile.id) {
return done('something', null)
}
return done(null, {'profile': profile, 'facebook_token': accessToken})
}))
return done(null, user);
} catch (err) {
done(err);
}
}
)
);
/**
* @FacebookTokenStrategy : The Facebook authentication strategy authenticates users using a Facebook
* account and OAuth 2.0 tokens.
*/
passport.use(
'facebookToken',
new FacebookTokenStrategy(
{
clientID: process.env.FACEBOOK_APP_ID,
clientSecret: process.env.FACEBOOK_APP_SECRET
},
async (accessToken, refreshToken, profile, cb) => {
try {
const user = await User.findOne({ 'facebook.id': profile.id });
if (user) {
return cb(null, user);
}
const {
id,
displayName,
name: { givenName, familyName },
emails
} = profile;
firstName: profile._json.first_name,
lastName: profile._json.last_name,
email: profile._json.email
};
User.create(user, function(err, user){
if(err){ return done(err, null); }
return done(err, user);
});
}
});
});
}));
passport.use(new FacebookTokenStrategy({
clientID: config.passport.facebook.clientID,
clientSecret: config.passport.facebook.clientSecret
},
function(accessToken, refreshToken, profile, done) {
User.findOrCreate({ facebookId: profile.id }, function (err, user) {
return done(err, user);
});
}));
}
code: 'E_AUTH_FAILED',
message: [profile.provider, ' auth failed'].join('')
});
return next(null, user, {});
})
.catch(next);
} else {
req.user.socialProfiles[profile.provider] = profile._json;
req.user.save(next);
}
}
passport.use(new LocalStrategy(_.assign({}, LOCAL_STRATEGY_CONFIG), _onLocalStrategyAuth));
passport.use(new JwtStrategy(_.assign({}, JWT_STRATEGY_CONFIG), _onJwtStrategyAuth));
passport.use(new FacebookTokenStrategy(_.assign({}, SOCIAL_STRATEGY_CONFIG), _onSocialStrategyAuth));
passport.use(new TwitterTokenStrategy(_.assign({}, SOCIAL_STRATEGY_CONFIG), _onSocialStrategyAuth));
passport.use(new VKontakteTokenStrategy(_.assign({}, SOCIAL_STRATEGY_CONFIG), _onSocialStrategyAuth));
passport.use(new FoursquareTokenStrategy(_.assign({}, SOCIAL_STRATEGY_CONFIG), _onSocialStrategyAuth));
passport.use(new GitHubTokenStrategy(_.assign({}, SOCIAL_STRATEGY_CONFIG), _onSocialStrategyAuth));
passport.use(new InstagramTokenStrategy(_.assign({}, SOCIAL_STRATEGY_CONFIG), _onSocialStrategyAuth));
passport.use(new PayPalTokenStrategy(_.assign({}, SOCIAL_STRATEGY_CONFIG), _onSocialStrategyAuth));
passport.use(new RedditTokenStrategy(_.assign({}, SOCIAL_STRATEGY_CONFIG), _onSocialStrategyAuth));
passport.use(new SoundCloudTokenStrategy(_.assign({}, SOCIAL_STRATEGY_CONFIG), _onSocialStrategyAuth));
passport.use(new WindowsLiveTokenStrategy(_.assign({}, SOCIAL_STRATEGY_CONFIG), _onSocialStrategyAuth));
passport.use(new TwitchTokenStrategy(_.assign({}, SOCIAL_STRATEGY_CONFIG), _onSocialStrategyAuth));
passport.use(new YandexTokenStrategy(_.assign({}, SOCIAL_STRATEGY_CONFIG), _onSocialStrategyAuth));
passport.use(new AmazonTokenStrategy(_.assign({}, SOCIAL_STRATEGY_CONFIG), _onSocialStrategyAuth));
passport.use(new GooglePlusTokenStrategy(_.assign({}, SOCIAL_STRATEGY_CONFIG), _onSocialStrategyAuth));
passport.use(new YahooTokenStrategy(_.assign({}, SOCIAL_STRATEGY_CONFIG), _onSocialStrategyAuth));
module.exports = function(passport) {
passport.serializeUser(serialize);
passport.deserializeUser(deserialize);
passport.use(new FacebookTokenStrategy({
clientID: 'facebook_id',
clientSecret: 'facebook_secrect_key',
redirectURL: 'facebook_redirect_url'
}, facebookAuth));
};