Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
state: 'success',
data: req.user.access_token
});
} else {
res.render('extenral_account_oauth', {
state: 'failure',
data: {
message: "Google+ authentication failed :("
}
});
}
});
// connect to current session
router.get('/auth/connect/google',
passport.authorize('google', {
scope : 'email'
})
);
// disconnect from current session
router.get('/auth/disconnect/google',
function (req, res) {
console.log('disconnect google');
if (!req.user) {
res.send(401, { reason: 'not-authenticated' });
} else {
var user = req.user;
user.google = undefined;
console.log('google info: ' + req.user.google);
user.save(function (err) {
if (err) {
router.get('/auth/github/callback', passport.authenticate('github', { failureRedirect: `${CLIENT_URL}/login` }), ((req, res) => {
// successfull authentication from github
res.redirect(`${CLIENT_URL}/verify_account`);
}));
// TWITTER
passport.use(new TwitterStrategy({
consumerKey: process.env.TWITTER_KEY,
consumerSecret: process.env.TWITTER_SECRET,
callbackURL: `${SERVER_URL}/connect/twitter/callback`
}, (token, tokenSecret, profile, done) => {
return done(null, profile)
}));
router.get('/connect/twitter', isAuthenticated, passport.authorize('twitter'));
router.get('/connect/twitter/callback',
passport.authorize('twitter', { failureRedirect: `${CLIENT_URL}/dashboard/preferences` }), ((req, res) => {
const { user, account } = req;
User.findOne({ githubId: user.githubId }, (err, updatedUser) => {
if (!err) {
updatedUser.social.twitter = account.username;
updatedUser.save();
res.redirect(`${CLIENT_URL}/dashboard/preferences`);
console.log('updated user with twitter handle');
} else {
console.log(err);
}
});
}));
});
}));
// LinkedIn
passport.use(new LinkedInStrategy({
consumerKey: process.env.LINKEDIN_KEY,
consumerSecret: process.env.LINKEDIN_SECRET,
callbackURL: `${SERVER_URL}/connect/linkedin/callback`
}, (token, tokenSecret, profile, done) => {
return done(null, profile)
}));
router.get('/connect/linkedin', isAuthenticated, passport.authorize('linkedin'));
router.get('/connect/linkedin/callback',
passport.authorize('linkedin', { failureRedirect: `${CLIENT_URL}/dashboard/preferences` }), ((req, res) => {
User.findOne({ githubId: req.user.githubId }, (err, updatedUser) => {
if (!err) {
updatedUser.social.linkedin = req.account.displayName;
updatedUser.save();
res.redirect(`${CLIENT_URL}/dashboard/preferences`);
console.log('updated user with linkedin handle');
} else {
res.staus(500).send(err.message);
}
});
}));
// logout user & redirect to home page
router.get('/logout', function(req, res){
req.logout();
router.get('/login/linkedin', passport.authenticate('linkedin', {
state: auth.state,
session: false,
}));
router.get(`/login${auth.linkedin.callbackURL}`, passport.authenticate('linkedin', {
failureRedirect: '/login',
session: false,
}), (req, res) => {
const expiresIn = 60 * 60 * 24 * 180; // 180 days
const token = jwt.sign(req.user, auth.jwt.secret, { expiresIn });
res.cookie('id_token', token, { maxAge: 1000 * expiresIn, httpOnly: true });
res.redirect('/login?callback=true');
});
router.get('/link/linkedin', passport.authorize('linkedin-link', {
state: auth.state,
session: false,
}));
router.get(`/link${auth.linkedin.callbackURL}`, passport.authorize('linkedin-link', {
session: false,
}), (req, res) => {
res.redirect('/profile');
});
router.get('/reload/linkedin', passport.authorize('linkedin-reload', {
state: auth.state,
session: false,
}));
router.get(`/reload${auth.linkedin.callbackURL}`, passport.authorize('linkedin-reload', {
state: auth.state,
session: false,
}));
router.get(`/link${auth.linkedin.callbackURL}`, passport.authorize('linkedin-link', {
session: false,
}), (req, res) => {
res.redirect('/profile');
});
router.get('/reload/linkedin', passport.authorize('linkedin-reload', {
state: auth.state,
session: false,
}));
router.get(`/reload${auth.linkedin.callbackURL}`, passport.authorize('linkedin-reload', {
session: false,
}), (req, res) => {
res.redirect('/profile');
});
router.get('/login/google', passport.authenticate('google', {
state: auth.state,
scope: ['profile', 'email'],
session: false,
}));
router.get(`/login${auth.google.callbackURL}`, passport.authenticate('google', {
failureRedirect: '/login',
session: false,
}), (req, res) => {
const expiresIn = 60 * 60 * 24 * 180; // 180 days
res.cookie('id_token', token, { maxAge: 1000 * expiresIn, httpOnly: true });
res.redirect('/login?callback=true');
});
router.get('/link/facebook', passport.authorize('facebook-link', {
scope: ['email', 'user_birthday'],
session: false,
}));
router.get(`/link${auth.facebook.callbackURL}`, passport.authorize('facebook-link', {
session: false,
}), (req, res) => {
res.redirect('/profile');
});
router.get('/reload/facebook', passport.authorize('facebook-reload', {
scope: ['email', 'user_birthday'],
session: false,
}));
router.get(`/reload${auth.facebook.callbackURL}`, passport.authorize('facebook-reload', {
session: false,
}), (req, res) => {
res.redirect('/profile');
});
router.get('/login/linkedin', passport.authenticate('linkedin', {
state: auth.state,
session: false,
}));
router.get(`/login${auth.linkedin.callbackURL}`, passport.authenticate('linkedin', {
req.flash('success', { msg: 'Tweet has been posted.' });
res.redirect('/api/twitter');
});
});
/**
* OAuth routes for API examples that require authorization.
*/
app.get('/auth/foursquare', passport.authorize('foursquare'));
app.get('/auth/foursquare/callback', passport.authorize('foursquare', { failureRedirect: '/api' }), function (req, res) {
res.redirect('/api/foursquare');
});
app.get('/auth/tumblr', passport.authorize('tumblr'));
app.get('/auth/tumblr/callback', passport.authorize('tumblr', { failureRedirect: '/api' }), function (req, res) {
res.redirect('/api/tumblr');
});
};