-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.js
125 lines (111 loc) · 3.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var env = process.env.NODE_ENV || 'production',
config = require('./config')[env];
var express = require('express'),
http = require('http'),
path = require('path'),
OAuth = require('oauth'),
passport = require('passport'),
mongoose = require('mongoose'),
FitbitStrategy = require('passport-fitbit').Strategy;
var app = express();
// All environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('TODO Random String: Fitbit is awesome!'));
app.use(express.session());
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// Development Environment
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
// Connect to database and initialize models
mongoose.connect(config.db);
require('./models/user');
var User = mongoose.model('User');
// Initialize controllers
var IndexController = require('./controllers/index'),
FitbitApiController = require('./controllers/fitbit-api'),
TwilioApiController = require('./controllers/twilio-api');
// Initialize OAuth
passport.serializeUser(function(user, done) {
console.log("serialize user", user);
done(null, user);
});
passport.deserializeUser(function(obj, done) {
console.log("deserialize obj", obj);
done(null, obj);
});
passport.use(new FitbitStrategy({
consumerKey: config.fitbitClientKey,
consumerSecret: config.fitbitClientSecret,
callbackURL: "http://" + config.host + "/auth/fitbit/callback"
},
function(token, tokenSecret, profile, done) {
// Store the user credentials
console.log('in passport use function ...');
User.update(
{ encodedId: profile.id },
{
encodedId: profile.id,
accessToken: token,
accessSecret: tokenSecret
},
{ upsert: true },
function(err, numberAffected) {
if (err) console.error(err);
console.log('User updated ' + numberAffected + ' records.');
}
);
console.log('connect to fitbit ...');
// Create a subscription.
var oauth = new OAuth.OAuth(
'https://api.fitbit.com/oauth/request_token',
'https://api.fitbit.com/oauth/access_token',
config.fitbitClientKey,
config.fitbitClientSecret,
'1.0',
null,
'HMAC-SHA1'
);
console.log('post to fitbit ...');
oauth.post(
'https://api.fitbit.com/1/user/-/apiSubscriptions/' + profile.id + '-all.json',
token,
tokenSecret,
null,
null,
function (err, data, res){
if (err) console.error(err);
console.log("Subscription creation attempt results:", require('util').inspect(data));
return done(null, profile);
}
);
conole.log('all done');
}
));
app.get('/auth/fitbit', passport.authenticate('fitbit'));
app.get('/auth/fitbit/callback',
passport.authenticate('fitbit', { failureRedirect: '/?error=auth_failed' }),
function(req, res) {
conole.log('fitbit called back ... get the phone data');
// Successful authentication, redirect home.
res.redirect('/phone');
}
);
// Index and Notification routes
app.get('/', IndexController.index);
app.get('/phone', IndexController.showUser);
app.post('/phone', IndexController.saveUser);
app.post('/notifications', FitbitApiController.notificationsReceived);
// Start the server
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});