-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
172 lines (148 loc) · 5.09 KB
/
app.js
File metadata and controls
172 lines (148 loc) · 5.09 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
var express = require('express'),
passport = require('passport'),
util = require('util'),
ip = require('ip'),
user = require('./db/user.js'),
Poll = require('./poller.js'),
FitbitStrategy = require('passport-fitbit').Strategy;
var port = 3000;
var FITBIT_CONSUMER_KEY = "7088513d14a84b16a93e4ab4775036b4";
var FITBIT_CONSUMER_SECRET = "1bec8db9c979486995307146559fe649";
// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session. Typically,
// this will be as simple as storing the user ID when serializing, and finding
// the user by ID when deserializing. However, since this example does not
// have a database of user records, the complete Fitbit profile is serialized
// and deserialized.
passport.serializeUser(function(user, done) {
//console.log('serializeUser:', user);
done(null, user);
});
passport.deserializeUser(function(obj, done) {
//console.log('deserializeUser:', user);
done(null, obj);
});
// Use the FitbitStrategy within Passport.
// Strategies in passport require a `verify` function, which accept
// credentials (in this case, a token, tokenSecret, and Fitbit profile), and
// invoke a callback with a user object.
passport.use(new FitbitStrategy({
consumerKey: FITBIT_CONSUMER_KEY,
consumerSecret: FITBIT_CONSUMER_SECRET,
callbackURL: 'http://' + ip.address() + ':3000/auth/fitbit/callback'
},
function(token, tokenSecret, profile, done) {
profile.fitbit_token = token
profile.fitbit_tokenSecret = tokenSecret;
user.findOrCreateNewUser(profile, function(err, user) {
var poll = new Poll(user.fitbit_token);
poll.start(user.fitbit);
return done(err, user);
});
}
));
var app = express();
// configure Express
app.configure(function() {
//app.use(express.logger());
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session({
secret: 'keyboard cat'
}));
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.get('/', function(req, res) {
console.log('In / !!!!')
if (!req.user) {
res.send('<!DOCTYPE html><html><head><title>Fritzbit</title>' +
'</head><body><a href="/auth/fitbit">Login</a></body></html>')
} else {
res.redirect('/index.html');
}
});
app.get('/account', ensureAuthenticated, function(req, res) {
console.log('In /account !!!!')
res.render('account', {
user: req.user
});
});
app.get('/login', function(req, res) {
console.log('In /login !!!!')
res.render('login', {
user: req.user
});
});
app.get('/fritzbit/data', function(req, res, next) {
console.log(req.user);
res.send(200, req.user);
});
app.post('user/:id/ratio/:ratio', function(req, res, next) {
console.log('adding ratio to user');
user.getUser(req.params.id, function(err, user) {
if (err) {
res.send(500, err);
} else {
user.website.ratio = req.params.ratio;
user.save(function(err, data) {
if (err) {
console.log('error saving to database', err);
res.send(500, err);
} else {
res.send(data);
}
});
}
});
});
// GET /auth/fitbit
// Use passport.authenticate() as route middleware to authenticate the
// request. The first step in Fitbit authentication will involve redirecting
// the user to fitbit.com. After authorization, Fitbit will redirect the user
// back to this application at /auth/fitbit/callback
app.get('/auth/fitbit', function(req, res, next) {
console.log('we are going to get here');
next();
},
passport.authenticate('fitbit'), function(req, res, next) {
console.log('we got here');
// The request will be redirected to Fitbit for authentication, so this
// function will not be called.
next();
});
// GET /auth/fitbit/callback
// Use passport.authenticate() as route middleware to authenticate the
// request. If authentication fails, the user will be redirected back to the
// login page. Otherwise, the primary route function function will be called,
// which, in this example, will redirect the user to the home page.
app.get('/auth/fitbit/callback',
passport.authenticate('fitbit', {
failureRedirect: '/login'
}),
function(req, res) {
res.redirect('/');
});
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
app.listen(port);
console.log('listening @: http://' + ip.address() + ':' + port);
// Simple route middleware to ensure user is authenticated.
// Use this route middleware on any resource that needs to be protected. If
// the request is authenticated (typically via a persistent login session),
// the request will proceed. Otherwise, the user will be redirected to the
// login page.
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login')
}