Reputation: 384
I've requested permission from apps.twitter.com and the authentication process is working successfully but twitter is not returning the user's email. Every other detail were returned (id, token, username, displayName) but Email remains null.
I will post some of my code below for context:
This is my mongoose database schema:
var mongoose = require('mongoose');
//Defining the database scheme.
var userSchema = mongoose.Schema ({
twitter: {
id: Number,
token: String,
email: {type:String, unique:true},
username: String,
displayName: String,
signupDate: { type: Date, default: Date.now }
}
});
//make the model available public
module.exports = mongoose.model('User', userSchema);
My passport.js:
//passport.js
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var TwitterStrategy = require('passport-twitter').Strategy;
// load up the user model
var User = require('../app/models/user');
// load the auth variables
var configAuth = require('./auth');
module.exports = function(passport) {
// used to serialize the user for the session
passport.serializeUser(function(user, done) {
done(null, user.id);
});
// used to deserialize the user
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
// code for login (use('local-login', new LocalStategy))
// code for signup (use('local-signup', new LocalStategy))
// code for facebook (use('facebook', new FacebookStrategy))
// =========================================================================
// TWITTER =================================================================
// =========================================================================
passport.use(new TwitterStrategy({
consumerKey : configAuth.twitterAuth.consumerKey,
consumerSecret : configAuth.twitterAuth.consumerSecret,
callbackURL : configAuth.twitterAuth.callbackURL,
userProfileURL: "https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true",
passReqToCallback : true,
},
function(token, tokenSecret, profile, done) {
// make the code asynchronous
// User.findOne won't fire until we have all our data back from Twitter
process.nextTick(function() {
User.findOne({ 'twitter.id' : profile.id }, 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);
// if the user is found then log them in
if (user) {
return done(null, user); // user found, return that user
} else {
// if there is no user, create them
var newUser = new User();
// set all of the user data that we need
newUser.twitter.id = profile.id;
newUser.twitter.token = token;
newUser.twitter.username = profile.username;
newUser.twitter.displayName = profile.displayName;
newUser.twitter.email = profile.email;
// save our user into the database
newUser.save(function(err) {
if (err)
throw err;
return done(null, newUser);
});
}
});
});
}));
};
I'd love to know what I'm getting wrong so I can return user's email successfully. Thanks
Upvotes: 1
Views: 2293
Reputation: 869
in passport-twitter you can implement this code
passport.use(
new TwitterStrategy (
...keys,
includeEmail: true,
)
)
you should config the app settings in your twitter developer to check the permission for account email.
Upvotes: 5
Reputation: 3729
Please check link
comment by @rvetere.
You get an email from the profile :
profile.emails[0].value
Also, check Your userProfileURL
export default new TwitterStrategy({
consumerKey : process.env.AUTH_TWITTER_CONSUMER_KEY,
consumerSecret : process.env.AUTH_TWITTER_CONSUMER_SECRET,
callbackURL : process.env.AUTH_TWITTER_CALLBACK_URL,
userProfileURL : 'https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true',
passReqToCallback : true,
},
Upvotes: 4