Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'passport-twitter' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
} else {
console.log("User not deserialized");
done(null, null);
}
});
});
});
// Use the TwitterStrategy within Passport.
// Strategies in passport require a `verify` function, which accept
// credentials (in this case, a token, tokenSecret, and Twitter profile), and
// invoke a callback with a user object.
twitterAuthn = new TwitterStrategy({
consumerKey: config.twitterConsumerKey,
consumerSecret: config.twitterConsumerSecret,
callbackURL: "http://openmhz.com/auth/twitter/callback"
},
function (token, tokenSecret, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
profile.token = token;
profile.tokenSecret = tokenSecret;
//console.log(profile);
// To keep the example simple, the user's Twitter profile is returned to
// represent the logged-in user. In a typical application, you would want
// to associate the Twitter account with a user record in your database,
// and return that user instead.
return done(null, profile);
});
constructor(req, config)
{
super(req, config);
if (!config.properties) {
config.properties = {};
}
if (!config.properties.id) {
config.properties.id = "id";
}
// passport
this.twitterStrategy = new TwitterStrategy({
consumerKey: config.consumerKey,
consumerSecret: config.consumerSecret,
callbackURL: config.callbackURL,
passReqToCallback: true
}, auth.buildPassportCallback(config, provider));
req.passport.use(this.twitterStrategy);
}
if (err)
throw err;
return done(null, newUser);
});
}
});
});
}));
// =========================================================================
// TWITTER =================================================================
// =========================================================================
passport.use(new TwitterStrategy({
consumerKey : config.twitterConsumerKey,
consumerSecret : config.twitterConsumerSecret,
callbackURL : config.twitterCallbackUrl,
passReqToCallback : true
},
function(req, token, tokenSecret, profile, done) {
process.nextTick(function() {
if (!req.user) {
User.findOne({ 'twitter.id' : profile.id }, function(err, user) {
if (err)
return done(err);
// check whether the strategy has already been set up
if (this.isTwitterStrategySetup) {
throw new Error('TwitterStrategy has already been set up');
}
const { configManager } = this.crowi;
const isTwitterEnabled = configManager.getConfig('crowi', 'security:passport-twitter:isEnabled');
// when disabled
if (!isTwitterEnabled) {
return;
}
debug('TwitterStrategy: setting up..');
passport.use(
new TwitterStrategy(
{
consumerKey: configManager.getConfig('crowi', 'security:passport-twitter:consumerKey'),
consumerSecret: configManager.getConfig('crowi', 'security:passport-twitter:consumerSecret'),
callbackURL: (this.crowi.appService.getSiteUrl() != null)
? urljoin(this.crowi.appService.getSiteUrl(), '/passport/twitter/callback') // auto-generated with v3.2.4 and above
: configManager.getConfig('crowi', 'security:passport-twitter:callbackUrl'), // DEPRECATED: backward compatible with v3.2.3 and below
skipUserProfile: false,
},
(accessToken, refreshToken, profile, done) => {
if (profile) {
return done(null, profile);
}
return done(null, false);
},
),
})
}
});
}
}
));
//function
// use twitter strategy
passport.use(new TwitterStrategy({
consumerKey: config.twitter.clientID
, consumerSecret: config.twitter.clientSecret
, callbackURL: config.twitter.callbackURL
, passReqToCallback: true
},
function(req,token, tokenSecret, profile, done) {
if(!req.user)
{
User.findOne({ 'twitter.id_str': profile.id }, function (err, user) {
if (err) { return done(err) }
if (!user) {
user = new User({twitter: profile._json});
user.name=user.twitter.screen_name;
user.loginCount++;
user.linkedin.profile = profile;
user.save(function(err) {
if (err)
return done(err);
return done(null, user);
});
}
});
}));
// =========================================================================
// TWITTER =================================================================
// =========================================================================
passport.use(new TwitterStrategy({
consumerKey : configAuth.twitterAuth.consumerKey,
consumerSecret : configAuth.twitterAuth.consumerSecret,
callbackURL : configAuth.twitterAuth.callbackURL,
passReqToCallback : true // allows us to pass in the req from our route (lets us check if a user is logged in or not)
},
function(req, token, tokenSecret, profile, done) {
// asynchronous
process.nextTick(function() {
// check if the user is already logged in
if (!req.user) {
User.findOne({ 'twitter.id' : profile.id }, function(err, user) {
providers.github))
// ---------------------------------------------------------------------------
// FACEBOOK
passport.use(new FacebookStrategy({
clientID: process.env.FACEBOOK_APP_ID,
clientSecret: process.env.FACEBOOK_APP_SECRET,
callbackURL: process.env.FACEBOOK_CALLBACK,
profileFields: process.env.FACEBOOK_SCOPE,
passReqToCallback: true
},
providers.facebook))
// ---------------------------------------------------------------------------
// TWITTER
passport.use(new TwitterStrategy({
consumerKey: process.env.TWITTER_API_KEY,
consumerSecret: process.env.TWITTER_API_SECRET,
callbackURL: process.env.TWITTER_CALLBACK,
passReqToCallback: true
},
providers.twitter))
// ---------------------------------------------------------------------------
// GOOGLE
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: process.env.GOOGLE_CALLBACK,
passReqToCallback: true
},
providers.google))
`${profile.name.givenName} ${profile.name.familyName}`;
const user = await login(req, 'facebook', profile, {
accessToken,
refreshToken,
});
done(null, user);
} catch (err) {
done(err);
}
},
),
);
// https://github.com/jaredhanson/passport-twitter
passport.use(
new TwitterStrategy(
{
consumerKey: process.env.TWITTER_KEY,
consumerSecret: process.env.TWITTER_SECRET,
callbackURL: '/login/twitter/return',
includeEmail: true,
includeStatus: false,
passReqToCallback: true,
},
async (req, token, tokenSecret, profile, done) => {
try {
if (profile.emails && profile.emails.length)
profile.emails[0].verified = true;
const user = await login(req, 'twitter', profile, {
token,
tokenSecret,
});
`${profile.name.givenName} ${profile.name.familyName}`;
const user = await login(req, 'facebook', profile, {
accessToken,
refreshToken,
});
done(null, user);
} catch (err) {
done(err);
}
},
),
);
// https://github.com/jaredhanson/passport-twitter
passport.use(
new TwitterStrategy(
{
consumerKey: process.env.TWITTER_KEY,
consumerSecret: process.env.TWITTER_SECRET,
callbackURL: '/login/twitter/return',
includeEmail: true,
includeStatus: false,
passReqToCallback: true,
},
async (req, token, tokenSecret, profile, done) => {
try {
if (profile.emails && profile.emails.length)
profile.emails[0].verified = true;
const user = await login(req, 'twitter', profile, {
token,
tokenSecret,
});
app.use(session({
secret: 'keyboard cat',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
passport.use(new TwitterStrategy({
consumerKey: config.twitterConsumerKey,
consumerSecret: config.twitterConsumerSecret,
callbackURL: config.twitterCallbackUrl
},
function(token, tokenSecret, profile, done) {
process.nextTick(function() {
console.log(token);
console.log(tokenSecret);
profile.oauth_token = token;
profile.oauth_token_secret = tokenSecret;
return done(null, profile);
});
}
));
app.get('/account', ensureAuthenticated, function(req, res) {