-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter.js
50 lines (40 loc) · 1.37 KB
/
twitter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var TwitterStrategy = require('passport-twitter').Strategy;
module.exports = function(app, db, passport) {
var config = {
consumerKey : process.env.TWITTER_CONSUMERKEY,
consumerSecret : process.env.TWITTER_CONSUMERSECRET,
callbackURL : process.env.BASE_IRI + '/auth/twitter/callback'
};
var handler = function(token, tokenSecret, profile, cb) {
var promise = db.get(
'_design/user/_view/twitter',
{key: profile.id}
).then(function(users) {
if (users.length) return users[0];
// if there is no user, create them
var user = {
'type': 'user'
};
user.twitter = {
id: profile.id,
token: token,
username: profile.username,
displayName: profile.displayName
};
// save our user into the database
return db.put(user);
});
promise.then(
function(user) {cb(null, user);},
function(err) {cb(err);}
);
};
passport.use(new TwitterStrategy(config, handler));
app.get('/auth/twitter', passport.authenticate('twitter'));
app.get('/auth/twitter/callback',
passport.authenticate('twitter', {
successRedirect : '/profile',
failureRedirect : '/'
})
);
};