-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
99 lines (84 loc) · 2.47 KB
/
app.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var config = require('./config.json');
var os = require('os');
var express = require('express');
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/sancha-3am');
var OAuth = require('oauth').OAuth
, oauth = new OAuth(
"https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/access_token",
config.twitter.consumer_key,
config.twitter.consumer_secret,
"1.0",
"http://" + config.hostname + "/auth/twitter/callback",
"HMAC-SHA1"
);
var User = new mongoose.Schema({
screen_name: { type: String },
access_token: { type: String },
access_token_secret: { type: String },
created: { type: Date, default: Date.now }
});
var User = db.model('User', User);
var app = express();
var cookieParser = require('cookie-parser');
var session = require('express-session');
app.use(cookieParser());
app.use(session({
secret: config.cookie_secret,
resave: false,
saveUninitialized: true,
cookie: { secure: !true }
}));
app.get('/auth/twitter', function(req, res) {
oauth.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results) {
if (error) {
console.log(error);
res.send("Authentication Failed!");
}
else {
req.session.oauth = {
token: oauth_token,
token_secret: oauth_token_secret
};
console.log(req.session.oauth);
res.redirect('https://twitter.com/oauth/authenticate?oauth_token='+oauth_token)
}
});
});
app.get('/auth/twitter/callback', function(req, res, next) {
if (req.session.oauth) {
req.session.oauth.verifier = req.query.oauth_verifier;
var oauth_data = req.session.oauth;
oauth.getOAuthAccessToken(
oauth_data.token,
oauth_data.token_secret,
oauth_data.verifier,
function(error, oauth_access_token, oauth_access_token_secret, results) {
if (error) {
console.log(error);
res.send("Failed");
}
else {
req.session.oauth.access_token = oauth_access_token;
req.session.oauth.access_token_secret = oauth_access_token_secret;
console.log(results, req.session.oauth);
User.find({ screen_name: results.screen_name }).remove().exec();
User.create({
screen_name: results.screen_name,
access_token: oauth_access_token,
access_token_secret: oauth_access_token_secret
});
res.send("Success!(" + results.screen_name + ")");
}
}
);
}
else {
res.send("Failed");
}
});
app.use(express.static('frontend/webroot'));
var server = app.listen(4000, function(){
console.log("PORT:" + server.address().port);
});