Skip to content

Commit 89098b3

Browse files
committed
wip: adding tumblr support
1 parent 7ba355b commit 89098b3

File tree

11 files changed

+177
-16
lines changed

11 files changed

+177
-16
lines changed

app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ app.configure(function(){
6767
app.set('google-oauth-key', config.oauth.google.key);
6868
app.set('google-oauth-secret', config.oauth.google.secret);
6969

70+
//google settings
71+
app.set('tumblr-oauth-key', config.oauth.tumblr.key);
72+
app.set('tumblr-oauth-secret', config.oauth.tumblr.secret);
73+
7074
//middleware
7175
app.use(express.logger('dev'));
7276
app.use(express.compress());

config.example.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,9 @@ exports.oauth = {
4242
google: {
4343
key: process.env.GOOGLE_OAUTH_KEY || '',
4444
secret: process.env.GOOGLE_OAUTH_SECRET || ''
45+
},
46+
tumblr: {
47+
key: process.env.TUMBLR_OAUTH_KEY || '',
48+
secret: process.env.TUMBLR_OAUTH_SECRET || ''
4549
}
4650
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"passport-github": "~0.1.5",
2020
"passport-twitter": "~1.0.2",
2121
"passport-google-oauth": "~0.1.5",
22+
"passport-tumblr": "^0.1.2",
2223
"helmet": "~0.2.0",
2324
"bcrypt": "~0.7.7"
2425
},

passport.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ exports = module.exports = function(app, passport) {
55
TwitterStrategy = require('passport-twitter').Strategy,
66
GitHubStrategy = require('passport-github').Strategy,
77
FacebookStrategy = require('passport-facebook').Strategy,
8-
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
8+
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy,
9+
TumblrStrategy = require('passport-tumblr').Strategy;
910

1011
passport.use(new LocalStrategy(
1112
function(username, password, done) {
@@ -102,6 +103,21 @@ exports = module.exports = function(app, passport) {
102103
));
103104
}
104105

106+
if (app.get('tumblr-oauth-key')) {
107+
passport.use(new TumblrStrategy({
108+
consumerKey: app.get('tumblr-oauth-key'),
109+
consumerSecret: app.get('tumblr-oauth-secret')
110+
},
111+
function(token, tokenSecret, profile, done) {
112+
done(null, false, {
113+
token: token,
114+
tokenSecret: tokenSecret,
115+
profile: profile
116+
});
117+
}
118+
));
119+
}
120+
105121
passport.serializeUser(function(user, done) {
106122
done(null, user._id);
107123
});

routes.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ exports = module.exports = function(app, passport) {
4949
app.get('/signup/facebook/callback/', require('./views/signup/index').signupFacebook);
5050
app.get('/signup/google/', passport.authenticate('google', { callbackURL: '/signup/google/callback/', scope: ['profile email'] }));
5151
app.get('/signup/google/callback/', require('./views/signup/index').signupGoogle);
52+
app.get('/signup/tumblr/', passport.authenticate('tumblr', { callbackURL: '/signup/tumblr/callback/' }));
53+
app.get('/signup/tumblr/callback/', require('./views/signup/index').signupTumblr);
5254

5355
//login/out
5456
app.get('/login/', require('./views/login/index').init);
@@ -69,6 +71,8 @@ exports = module.exports = function(app, passport) {
6971
app.get('/login/facebook/callback/', require('./views/login/index').loginFacebook);
7072
app.get('/login/google/', passport.authenticate('google', { callbackURL: '/login/google/callback/', scope: ['profile email'] }));
7173
app.get('/login/google/callback/', require('./views/login/index').loginGoogle);
74+
app.get('/login/tumblr/', passport.authenticate('tumblr', { callbackURL: '/login/tumblr/callback/', scope: ['profile email'] }));
75+
app.get('/login/tumblr/callback/', require('./views/login/index').loginTumblr);
7276

7377
//admin
7478
app.all('/admin*', ensureAuthenticated);
@@ -163,6 +167,9 @@ exports = module.exports = function(app, passport) {
163167
app.get('/account/settings/google/', passport.authenticate('google', { callbackURL: '/account/settings/google/callback/', scope: ['profile email'] }));
164168
app.get('/account/settings/google/callback/', require('./views/account/settings/index').connectGoogle);
165169
app.get('/account/settings/google/disconnect/', require('./views/account/settings/index').disconnectGoogle);
170+
app.get('/account/settings/tumblr/', passport.authenticate('tumblr', { callbackURL: '/account/settings/tumblr/callback/' }));
171+
app.get('/account/settings/tumblr/callback/', require('./views/account/settings/index').connectTumblr);
172+
app.get('/account/settings/tumblr/disconnect/', require('./views/account/settings/index').disconnectTumblr);
166173

167174
//route not found
168175
app.all('*', require('./views/http/index').http404);

views/account/settings/index.jade

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ block body
1717
div#identity
1818
div#password
1919
div.col-sm-3
20-
if oauthTwitter || oauthGitHub || oauthFacebook || oauthGoogle
20+
if oauthTwitter || oauthGitHub || oauthFacebook || oauthGoogle || oauthTumblr
2121
legend Social Connections
2222
if oauthMessage
2323
div.alerts
@@ -60,6 +60,15 @@ block body
6060
a.btn.btn-block.btn-default(href='/account/settings/google/')
6161
i.fa.fa-google-plus-square.fa-lg
6262
| Connect Google
63+
if oauthTumblr
64+
if oauthTumblrActive
65+
a.btn.btn-block.btn-danger(href='/account/settings/tumblr/disconnect/')
66+
i.fa.fa-tumblr-square.fa-lg
67+
| Disconnect Tumblr
68+
else
69+
a.btn.btn-block.btn-default(href='/account/settings/tumblr/')
70+
i.fa.fa-tumblr-square.fa-lg
71+
| Connect Tumblr
6372

6473
script(type='text/template', id='tmpl-details')
6574
fieldset

views/account/settings/index.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var renderSettings = function(req, res, next, oauthMessage) {
1515
};
1616

1717
var getUserData = function(callback) {
18-
req.app.db.models.User.findById(req.user.id, 'username email twitter.id github.id facebook.id google.id').exec(function(err, user) {
18+
req.app.db.models.User.findById(req.user.id, 'username email twitter.id github.id facebook.id google.id tumblr.id').exec(function(err, user) {
1919
if (err) {
2020
callback(err, null);
2121
}
@@ -43,7 +43,9 @@ var renderSettings = function(req, res, next, oauthMessage) {
4343
oauthFacebook: !!req.app.get('facebook-oauth-key'),
4444
oauthFacebookActive: outcome.user.facebook ? !!outcome.user.facebook.id : false,
4545
oauthGoogle: !!req.app.get('google-oauth-key'),
46-
oauthGoogleActive: outcome.user.google ? !!outcome.user.google.id : false
46+
oauthGoogleActive: outcome.user.google ? !!outcome.user.google.id : false,
47+
oauthTumblr: !!req.app.get('tumblr-oauth-key'),
48+
oauthTumblrActive: outcome.user.tumblr ? !!outcome.user.tumblr.id : false
4749
});
4850
};
4951

@@ -162,6 +164,33 @@ exports.connectGoogle = function(req, res, next){
162164
})(req, res, next);
163165
};
164166

167+
exports.connectTumblr = function(req, res, next){
168+
req._passport.instance.authenticate('tumblr', { callbackURL: '/account/settings/tumblr/callback/' }, function(err, user, info) {
169+
if (!info || !info.profile) {
170+
return res.redirect('/account/settings/');
171+
}
172+
173+
req.app.db.models.User.findOne({ 'tumblr.id': info.profile.id, _id: { $ne: req.user.id } }, function(err, user) {
174+
if (err) {
175+
return next(err);
176+
}
177+
178+
if (user) {
179+
renderSettings(req, res, next, 'Another user has already connected with that Tumblr account.');
180+
}
181+
else {
182+
req.app.db.models.User.findByIdAndUpdate(req.user.id, { 'tumblr.id': info.profile.id }, function(err, user) {
183+
if (err) {
184+
return next(err);
185+
}
186+
187+
res.redirect('/account/settings/');
188+
});
189+
}
190+
});
191+
})(req, res, next);
192+
};
193+
165194
exports.disconnectTwitter = function(req, res, next){
166195
req.app.db.models.User.findByIdAndUpdate(req.user.id, { twitter: { id: undefined } }, function(err, user) {
167196
if (err) {
@@ -202,6 +231,16 @@ exports.disconnectGoogle = function(req, res, next){
202231
});
203232
};
204233

234+
exports.disconnectTumblr = function(req, res, next){
235+
req.app.db.models.User.findByIdAndUpdate(req.user.id, { tumblr: { id: undefined } }, function(err, user) {
236+
if (err) {
237+
return next(err);
238+
}
239+
240+
res.redirect('/account/settings/');
241+
});
242+
};
243+
205244
exports.update = function(req, res, next){
206245
var workflow = req.app.utility.workflow(req, res);
207246

views/login/index.jade

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ block body
1212
div.page-header
1313
h1 Sign In
1414
div#login
15-
if oauthTwitter || oauthGitHub || oauthFacebook || oauthGoogle
15+
if oauthTwitter || oauthGitHub || oauthFacebook || oauthGoogle || oauthTumblr
1616
hr
1717
p Or sign in using...
1818
if oauthMessage
@@ -40,6 +40,10 @@ block body
4040
a.btn.btn-info(href='/login/google/')
4141
i.fa.fa-google-plus-square.fa-lg
4242
| Google
43+
if oauthTumblr
44+
a.btn.btn-info(href='/login/tumblr/')
45+
i.fa.fa-tumblr-square.fa-lg
46+
| Tumblr
4347

4448
script(type='text/template', id='tmpl-login')
4549
form

views/login/index.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ exports.init = function(req, res){
1919
oauthTwitter: !!req.app.get('twitter-oauth-key'),
2020
oauthGitHub: !!req.app.get('github-oauth-key'),
2121
oauthFacebook: !!req.app.get('facebook-oauth-key'),
22-
oauthGoogle: !!req.app.get('google-oauth-key')
22+
oauthGoogle: !!req.app.get('google-oauth-key'),
23+
oauthTumblr: !!req.app.get('tumblr-oauth-key')
2324
});
2425
}
2526
};
@@ -132,7 +133,8 @@ exports.loginTwitter = function(req, res, next){
132133
oauthTwitter: !!req.app.get('twitter-oauth-key'),
133134
oauthGitHub: !!req.app.get('github-oauth-key'),
134135
oauthFacebook: !!req.app.get('facebook-oauth-key'),
135-
oauthGoogle: !!req.app.get('google-oauth-key')
136+
oauthGoogle: !!req.app.get('google-oauth-key'),
137+
oauthTumblr: !!req.app.get('tumblr-oauth-key')
136138
});
137139
}
138140
else {
@@ -165,7 +167,8 @@ exports.loginGitHub = function(req, res, next){
165167
oauthTwitter: !!req.app.get('twitter-oauth-key'),
166168
oauthGitHub: !!req.app.get('github-oauth-key'),
167169
oauthFacebook: !!req.app.get('facebook-oauth-key'),
168-
oauthGoogle: !!req.app.get('google-oauth-key')
170+
oauthGoogle: !!req.app.get('google-oauth-key'),
171+
oauthTumblr: !!req.app.get('tumblr-oauth-key')
169172
});
170173
}
171174
else {
@@ -198,7 +201,8 @@ exports.loginFacebook = function(req, res, next){
198201
oauthTwitter: !!req.app.get('twitter-oauth-key'),
199202
oauthGitHub: !!req.app.get('github-oauth-key'),
200203
oauthFacebook: !!req.app.get('facebook-oauth-key'),
201-
oauthGoogle: !!req.app.get('google-oauth-key')
204+
oauthGoogle: !!req.app.get('google-oauth-key'),
205+
oauthTumblr: !!req.app.get('tumblr-oauth-key')
202206
});
203207
}
204208
else {
@@ -231,7 +235,42 @@ exports.loginGoogle = function(req, res, next){
231235
oauthTwitter: !!req.app.get('twitter-oauth-key'),
232236
oauthGitHub: !!req.app.get('github-oauth-key'),
233237
oauthFacebook: !!req.app.get('facebook-oauth-key'),
234-
oauthGoogle: !!req.app.get('google-oauth-key')
238+
oauthGoogle: !!req.app.get('google-oauth-key'),
239+
oauthTumblr: !!req.app.get('tumblr-oauth-key')
240+
});
241+
}
242+
else {
243+
req.login(user, function(err) {
244+
if (err) {
245+
return next(err);
246+
}
247+
248+
res.redirect(getReturnUrl(req));
249+
});
250+
}
251+
});
252+
})(req, res, next);
253+
};
254+
255+
exports.loginTumblr = function(req, res, next){
256+
req._passport.instance.authenticate('tumblr', { callbackURL: '/login/tumblr/callback/' }, function(err, user, info) {
257+
if (!info || !info.profile) {
258+
return res.redirect('/login/');
259+
}
260+
261+
req.app.db.models.User.findOne({ 'tumblr.id': info.profile.id }, function(err, user) {
262+
if (err) {
263+
return next(err);
264+
}
265+
266+
if (!user) {
267+
res.render('login/index', {
268+
oauthMessage: 'No users found linked to your Tumblr account. You may need to create an account first.',
269+
oauthTwitter: !!req.app.get('twitter-oauth-key'),
270+
oauthGitHub: !!req.app.get('github-oauth-key'),
271+
oauthFacebook: !!req.app.get('facebook-oauth-key'),
272+
oauthGoogle: !!req.app.get('google-oauth-key'),
273+
oauthTumblr: !!req.app.get('tumblr-oauth-key')
235274
});
236275
}
237276
else {

views/signup/index.jade

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ block body
1515
div.page-header
1616
h1 Sign Up
1717
div#signup
18-
if oauthTwitter || oauthGitHub || oauthFacebook || oauthGoogle
18+
if oauthTwitter || oauthGitHub || oauthFacebook || oauthGoogle || oauthTumblr
1919
hr
2020
p Or sign up using...
2121
if oauthMessage
@@ -42,6 +42,10 @@ block body
4242
a.btn.btn-info(href='/signup/google/')
4343
i.fa.fa-google-plus-square.fa-lg
4444
| Google
45+
if oauthTumblr
46+
a.btn.btn-info(href='/signup/tumblr/')
47+
i.fa.fa-tumblr-square.fa-lg
48+
| Tumblr
4549
div.col-sm-6.marketing
4650
div.page-header
4751
h1 Campy Benefits

views/signup/index.js

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ exports.init = function(req, res){
1010
oauthTwitter: !!req.app.get('twitter-oauth-key'),
1111
oauthGitHub: !!req.app.get('github-oauth-key'),
1212
oauthFacebook: !!req.app.get('facebook-oauth-key'),
13-
oauthGoogle: !!req.app.get('google-oauth-key')
13+
oauthGoogle: !!req.app.get('google-oauth-key'),
14+
oauthTumblr: !!req.app.get('tumblr-oauth-key')
1415
});
1516
}
1617
};
@@ -201,7 +202,8 @@ exports.signupTwitter = function(req, res, next) {
201202
oauthTwitter: !!req.app.get('twitter-oauth-key'),
202203
oauthGitHub: !!req.app.get('github-oauth-key'),
203204
oauthFacebook: !!req.app.get('facebook-oauth-key'),
204-
oauthGoogle: !!req.app.get('google-oauth-key')
205+
oauthGoogle: !!req.app.get('google-oauth-key'),
206+
oauthTumblr: !!req.app.get('tumblr-oauth-key')
205207
});
206208
}
207209
});
@@ -229,7 +231,8 @@ exports.signupGitHub = function(req, res, next) {
229231
oauthTwitter: !!req.app.get('twitter-oauth-key'),
230232
oauthGitHub: !!req.app.get('github-oauth-key'),
231233
oauthFacebook: !!req.app.get('facebook-oauth-key'),
232-
oauthGoogle: !!req.app.get('google-oauth-key')
234+
oauthGoogle: !!req.app.get('google-oauth-key'),
235+
oauthTumblr: !!req.app.get('tumblr-oauth-key')
233236
});
234237
}
235238
});
@@ -256,7 +259,8 @@ exports.signupFacebook = function(req, res, next) {
256259
oauthTwitter: !!req.app.get('twitter-oauth-key'),
257260
oauthGitHub: !!req.app.get('github-oauth-key'),
258261
oauthFacebook: !!req.app.get('facebook-oauth-key'),
259-
oauthGoogle: !!req.app.get('google-oauth-key')
262+
oauthGoogle: !!req.app.get('google-oauth-key'),
263+
oauthTumblr: !!req.app.get('tumblr-oauth-key')
260264
});
261265
}
262266
});
@@ -283,7 +287,37 @@ exports.signupGoogle = function(req, res, next) {
283287
oauthTwitter: !!req.app.get('twitter-oauth-key'),
284288
oauthGitHub: !!req.app.get('github-oauth-key'),
285289
oauthFacebook: !!req.app.get('facebook-oauth-key'),
286-
oauthGoogle: !!req.app.get('google-oauth-key')
290+
oauthGoogle: !!req.app.get('google-oauth-key'),
291+
oauthTumblr: !!req.app.get('tumblr-oauth-key')
292+
});
293+
}
294+
});
295+
})(req, res, next);
296+
};
297+
298+
exports.signupTumblr = function(req, res, next) {
299+
req._passport.instance.authenticate('tumblr', { callbackURL: '/signup/tumblr/callback/' }, function(err, user, info) {
300+
if (!info || !info.profile) {
301+
return res.redirect('/signup/');
302+
}
303+
304+
console.log(info);
305+
req.app.db.models.User.findOne({ 'tumblr.id': info.profile.id }, function(err, user) {
306+
if (err) {
307+
return next(err);
308+
}
309+
if (!user) {
310+
req.session.socialProfile = info.profile;
311+
res.render('signup/social', { email: info.profile.emails && info.profile.emails[0].value || '' });
312+
}
313+
else {
314+
res.render('signup/index', {
315+
oauthMessage: 'We found a user linked to your Tumblr account.',
316+
oauthTwitter: !!req.app.get('twitter-oauth-key'),
317+
oauthGitHub: !!req.app.get('github-oauth-key'),
318+
oauthFacebook: !!req.app.get('facebook-oauth-key'),
319+
oauthGoogle: !!req.app.get('google-oauth-key'),
320+
oauthTumblr: !!req.app.get('tumblr-oauth-key')
287321
});
288322
}
289323
});

0 commit comments

Comments
 (0)