Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update Facebook post route to return 422 instead of timing out if fb profile has no email #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 48 additions & 46 deletions dist/auth/facebook/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,57 +54,59 @@ function initialize(_user, callback) {
router.post('/', function (req, res, next) {
var fields = ['id', 'first_name', 'last_name', 'email', 'picture'];
_fbgraph2.default.get('me?fields=' + fields.join() + '&access_token=' + req.body.accessToken, function (err, profile) {
if (!profile.email) {
return;
}

_user.findOne({ email: profile.email.toLowerCase() }).then(function (user) {
if (!user) {
// not registered
//generate a random password for using Facebook login

var randomPassword = _crypto2.default.randomBytes(16).toString('base64');

_user.create({
firstName: profile.first_name,
lastName: profile.last_name,
email: profile.email.toLowerCase(),
facebookID: profile.id,
password: randomPassword,
provider: 'facebook',
socialProfiles: {
facebook: {
accessToken: req.body.accessToken,
avatar: profile.picture.data.url
if (!profile.email) {
res.status(422).json({ message: 'Facebook profile did not return an email address' });
} else {

_user.findOne({ email: profile.email.toLowerCase() }).then(function (user) {
if (!user) {
// not registered
//generate a random password for using Facebook login

var randomPassword = _crypto2.default.randomBytes(16).toString('base64');

_user.create({
firstName: profile.first_name,
lastName: profile.last_name,
email: profile.email.toLowerCase(),
facebookID: profile.id,
password: randomPassword,
provider: 'facebook',
socialProfiles: {
facebook: {
accessToken: req.body.accessToken,
avatar: profile.picture.data.url
}
}
}).then(function (result) {
callback(result, profile);
result = result.toObject();
var token = auth.signToken(result._id);
res.json({ token: token });
}).catch(function (err) {
console.log(err);
res.status(400).json({ message: 'Could not create user, please try again.' });
});
} else {
// is registered
if (!user.socialProfiles) {
user.socialProfiles = { facebook: {} };
}
}).then(function (result) {
callback(result, profile);
result = result.toObject();
var token = auth.signToken(result._id);
res.json({ token: token });
}).catch(function (err) {
console.log(err);
res.status(400).json({ message: 'Could not create user, please try again.' });
});
} else {
// is registered
if (!user.socialProfiles) {
user.socialProfiles = { facebook: {} };
}

user.socialProfiles.facebook.id = profile.id;
user.socialProfiles.facebook.accessToken = req.body.accessToken;
user.save();
user.socialProfiles.facebook.id = profile.id;
user.socialProfiles.facebook.accessToken = req.body.accessToken;
user.save();

callback(user, profile);
var token = auth.signToken(user._id);
res.json({ token: token });
}
}).catch(function (err) {
console.log(err);
res.status(400).json({ message: 'Something went wrong, please try again.' });
});
callback(user, profile);
var token = auth.signToken(user._id);
res.json({ token: token });
}
}).catch(function (err) {
console.log(err);
res.status(400).json({ message: 'Something went wrong, please try again.' });
});
}
});
});
}
Expand Down
Loading