Add linkedin support for login

This commit is contained in:
Harshad Kale
2014-01-25 12:03:26 -06:00
parent d59bf8645d
commit 58d2cbf706
7 changed files with 54 additions and 1 deletions

View File

@@ -23,7 +23,8 @@ var UserSchema = new Schema({
facebook: {},
twitter: {},
github: {},
google: {}
google: {},
linkedin: {}
});
/**

View File

@@ -63,4 +63,14 @@ module.exports = function(app, passport) {
failureRedirect: '/signin'
}), users.authCallback);
// Setting the linkedin oauth routes
app.get('/auth/linkedin', passport.authenticate('linkedin', {
failureRedirect: '/signin',
scope: [ 'r_emailaddress' ]
}), users.signin);
app.get('/auth/linkedin/callback', passport.authenticate('linkedin', {
failureRedirect: '/siginin'
}), users.authCallback);
};

View File

@@ -11,6 +11,8 @@ block content
img(src="/img/icons/twitter.png")
a(href="/auth/google")
img(src="/img/icons/google.png")
a(href="/auth/linkedin")
img(src="/img/icons/linkedin.png")
.col-md-6
if message && message.length
.fade.in.alert.alert-error

View File

@@ -24,5 +24,10 @@ module.exports = {
clientID: "APP_ID",
clientSecret: "APP_SECRET",
callbackURL: "http://localhost:3000/auth/google/callback"
},
linkedin: {
clientID: "API_KEY",
clientSecret: "SECRET_KEY",
callbackURL: "http://localhost:3000/auth/linkedin/callback"
}
}

View File

@@ -24,5 +24,10 @@ module.exports = {
clientID: "APP_ID",
clientSecret: "APP_SECRET",
callbackURL: "http://localhost:3000/auth/google/callback"
},
linkedin: {
clientID: "API_KEY",
clientSecret: "SECRET_KEY",
callbackURL: "http://localhost:3000/auth/linkedin/callback"
}
}

View File

@@ -6,6 +6,7 @@ var mongoose = require('mongoose'),
FacebookStrategy = require('passport-facebook').Strategy,
GitHubStrategy = require('passport-github').Strategy,
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy,
LinkedinStrategy = require('passport-linkedin').Strategy,
User = mongoose.model('User'),
config = require('./config');
@@ -174,4 +175,33 @@ module.exports = function(passport) {
});
}
));
// use linkedin strategy
passport.use(new LinkedinStrategy({
consumerKey: config.linkedin.clientID,
consumerSecret: config.linkedin.clientSecret,
callbackURL: config.linkedin.callbackURL,
profileFields: ['id', 'first-name', 'last-name', 'email-address']
},
function(accessToken, refreshToken, profile, done) {
User.findOne({
'linkedin.id': profile.id
}, function (err, user) {
if (!user) {
user = new User({
name: profile.displayName,
email: profile.emails[0].value,
username: profile.emails[0].value,
provider: 'linkedin'
});
user.save(function (err) {
if (err) console.log(err);
return done(err, user);
});
} else {
return done(err, user)
}
});
}
));
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB