fixing fitbit

This commit is contained in:
Martin Donnelly 2016-04-11 15:14:19 +01:00
parent 225216bc98
commit b84b6843d8
2 changed files with 8 additions and 20 deletions

View File

@ -8,7 +8,7 @@ var logger = require('log4js').getLogger();
require('sugar-date');
var fitbit = new Fitbit(config.fitbit);
var tokenFile = '../fb-token.json';
var tokenFile = '../../fb-token.json';
module.exports = {

View File

@ -112,23 +112,7 @@ app.route('/poly').get(polys);
var tfile = 'fb-token.json';
var persist = {
read: function( filename, cb ) {
fs.readFile( filename, { encoding: 'utf8', flag: 'r' }, function( err, data ) {
if ( err ) return cb( err );
try {
var token = JSON.parse( data );
cb( null, token );
} catch( err ) {
cb( err );
}
});
},
write: function( filename, token, cb ) {
console.log( 'persisting new token:', JSON.stringify( token ) );
fs.writeFile( filename, JSON.stringify( token ), cb );
}
};
// Instanciate a fitbit client. See example config below.
//
@ -147,11 +131,15 @@ app.get('/fitbit', function (req, res) {
app.get('/fitbit_auth_callback', function (req, res, next) {
var code = req.query.code;
fitbit.fetchToken( code, function( err, token ) {
if ( err ) return next( err );
if ( err ) {
return next( err );
}
// persist the token
jsonfile.writeFile( tfile, token, function( err ) {
if ( err ) return next( err );
if ( err ) {
return next( err );
}
res.redirect( '/fb-profile' );
});
});