Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var config = require('../config');
var passport = require('passport');
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
passport.use(new GoogleStrategy(
config.googleAuth,
function(accessToken, refreshToken, profile, done) {
// console.log(profile);
// User.findOrCreate({
// googleId: profile.id
// }, function(err, user) {
// return done(err, user);
// });
}
));
last_name: profile.displayName.split(' ').slice(1).join(' '),
email: `${profile.id}@twitter.com` // Until there is no way to retrieve email from twitter API
}, (err, user, info) => {
if (err) {
return done(err);
}
if (user) {
user.info = _getThirdPartyInfo(profile);
}
done(null, user, info);
});
}));
}
if (CONFIG.AUTH.GOOGLE.ENABLED) {
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: '/auth/google/callback'
}, (accessToken, refreshToken, profile, done) => {
// Dont forget to enable Google+ API in Developer Console in order to get user's information
AuthService.resolveUser({
google_id: profile.id,
name: profile.name.givenName,
last_name: profile.name.familyName,
email: profile.emails[0].value
}, (err, user, info) => {
if (err) {
return done(err);
}
if (user) {
user.info = _getThirdPartyInfo(profile);
const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy
const User = require('../db/models/user')
const strategy = new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: '/auth/google/callback'
},
function(token, tokenSecret, profile, done) {
// testing
console.log('===== GOOGLE PROFILE =======')
console.log(profile)
console.log('======== END ===========')
// code
const { id, name, photos } = profile
User.findOne({ googleId: id }, (err, userMatch) => {
// handle errors here:
if (err) {
console.log('Error!! trying to find user with googleId')
try {
const user = await User.signInOrSignUp({
googleId: profile.id,
email,
googleToken: { accessToken, refreshToken },
displayName: profile.displayName,
avatarUrl,
});
verified(null, user);
} catch (err) {
verified(err);
console.log(err); // eslint-disable-line
}
};
passport.use(
new Strategy(
{
clientID: process.env.Google_clientID,
clientSecret: process.env.Google_clientSecret,
callbackURL: `${ROOT_URL}/oauth2callback`,
},
verify,
),
);
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id, User.publicFields(), (err, user) => {
done(err, user);
try {
const user = await User.signInOrSignUp({
googleId: profile.id,
email,
googleToken: { accessToken, refreshToken },
displayName: profile.displayName,
avatarUrl,
});
verified(null, user);
} catch (err) {
verified(err);
console.log(err); // eslint-disable-line
}
};
passport.use(
new Strategy(
{
clientID: process.env.Google_clientID,
clientSecret: process.env.Google_clientSecret,
callbackURL: `${ROOT_URL}/oauth2callback`,
userProfileURL: 'https://www.googleapis.com/oauth2/v3/userinfo',
},
verify,
),
);
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id, User.publicFields(), (err, user) => {
try {
const user = await User.signInOrSignUp({
googleId: profile.id,
email,
googleToken: { accessToken, refreshToken },
displayName: profile.displayName,
avatarUrl,
});
verified(null, user);
} catch (err) {
verified(err);
console.log(err); // eslint-disable-line
}
};
passport.use(
new Strategy(
{
clientID: process.env.Google_clientID,
clientSecret: process.env.Google_clientSecret,
callbackURL: `${ROOT_URL}/oauth2callback`,
userProfileURL: 'https://www.googleapis.com/oauth2/v3/userinfo',
},
verify,
),
);
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id, User.publicFields(), (err, user) => {
try {
const user = await User.signInOrSignUp({
googleId: profile.id,
email,
googleToken: { accessToken, refreshToken },
displayName: profile.displayName,
avatarUrl,
});
verified(null, user);
} catch (err) {
verified(err);
console.log(err); // eslint-disable-line
}
};
passport.use(
new Strategy(
{
clientID: process.env.Google_clientID,
clientSecret: process.env.Google_clientSecret,
callbackURL: `${ROOT_URL}/oauth2callback`,
},
verify,
),
);
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id, User.publicFields(), (err, user) => {
done(err, user);
var lazySetupPassport = function(req) {
passportIsSet = true;
var protocol = (req.connection.encrypted || req.headers['x-forwarded-proto'] === "https" ) ? "https" : "http";
//not doing anything with this:
//it will try to serialize the users in the session.
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
var callbackUrl = protocol + "://" + req.headers.host + "/auth/google/callback";
passport.use(new GoogleStrategy({
clientID: config.appID, clientSecret: config.appSecret, callbackURL: callbackUrl
}, function(accessToken, refreshToken, profile, done) {
var validEmail = validateUser(profile);
if (!validEmail) {
done(null, false, { message: 'not an authorized email ' + profile.emails[0] });
} else {
done(null, profile);
}
}));
app.get('/auth/google', passport.authenticate('google'
, { scope: scope, 'approvalPrompt': 'force'/*, 'accessType': 'offline'*/ })
, function(req, res) {
// The request will be redirected to Google for authentication, so
// this function will not be called.
});
// have a database of user records, the complete Google profile is
// serialized and deserialized.
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
// Use the GoogleStrategy within Passport.
// Strategies in Passport require a `verify` function, which accept
// credentials (in this case, an accessToken, refreshToken, and Google
// profile), and invoke a callback with a user object.
passport.use(new GoogleStrategy({
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: "http://127.0.0.1:3000/auth/google/callback"
},
function(accessToken, refreshToken, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
// To keep the example simple, the user's Google profile is returned to
// represent the logged-in user. In a typical application, you would want
// to associate the Google account with a user record in your database,
// and return that user instead.
return done(null, profile);
});
}
));
.then(user => {
if (user) done(null, user)
else done(null, false)
})
.catch(done)
},
),
)
} else {
throw new Error('JWT_SECRET must be configured to allow authentication.')
}
// Google first
if (config.auth.google.clientID && config.auth.google.clientSecret) {
passport.use(
new GoogleStrategy(
{
clientID: config.auth.google.clientID,
clientSecret: config.auth.google.clientSecret,
callbackURL: `${config.url + config.apiPrefix}auth/google/callback`,
},
verifyOauthUser,
),
)
}
// then ORCID
if (config.auth.orcid.clientID && config.auth.orcid.clientSecret) {
passport.use(
new ORCIDStrategy(
{
sandbox: !!config.auth.orcid.apiURI,