Skip to content

Commit 6205cd4

Browse files
committed
added example auth server
1 parent 716bc92 commit 6205cd4

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

example/auth_server.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var express = require('express');
2+
var bodyParser = require('body-parser');
3+
var FitbitClient = require('fitbit-client-oauth2');
4+
5+
var app = express();
6+
7+
var clientId = 'YOUR_CLIENT_ID';
8+
var clientSecret = 'YOUR_CLIENT_SECRET';
9+
10+
var client = new FitbitClient(clientId, clientSecret);
11+
var redirect_uri = 'http://localhost:3000/auth/fitbit/callback';
12+
13+
app.use(bodyParser());
14+
15+
app.get('/auth/fitbit', function(req, res) {
16+
17+
var auth_url = client.getAuthorizationUrl('http://localhost:3000/auth/fitbit/callback');
18+
19+
res.redirect(auth_url);
20+
21+
});
22+
23+
app.get('/auth/fitbit/callback', function(req, res, next) {
24+
25+
client.getToken(req.query.code, redirect_uri)
26+
.then(function(token) {
27+
28+
// ... save your token on session or db ...
29+
30+
// then redirect
31+
//res.redirect(302, '/user');
32+
33+
res.send(token);
34+
35+
})
36+
.catch(function(err) {
37+
// something went wrong.
38+
res.send(500, err);
39+
40+
});
41+
42+
});
43+
44+
app.listen(3000);

0 commit comments

Comments
 (0)