'use strict';

const hapi = require('hapi');
const oauth = require('./lib/oauth.js');
const config = require('./config.js');

const server = new hapi.Server();
server.connection({
    host: 'localhost',
    port: 3000
});

server.route({
    method: 'GET',
    path: '/fitbit-authorization',
    handler: function (request, reply) {
        // Handle implicit grant flow
        if (request.query.access_token === undefined) {
            console.log('No accesss token detected.');
            console.log('Sending user through Fitbit authorization.');

            let authorizationUrl = oauth.getFitbitImplicitAccessToken(config.appCredentials.dev.fitbit);
            reply('Redirecting to Fitbit authorization...').redirect(authorizationUrl);
        } else if (request.query.access_token !== undefined) {
            let accessToken = request.query.access_token;
            console.log(`Access token found: ${accessToken}`);
        }
    }
});

server.route({
    method: 'GET',
    path: '/slack-authorization',
    handler: function (request, reply) {
        // Handles Slack authorizaiton through Authorization Code Grant Flow
        if (request.query.code === undefined) {
            console.log('No authorization code found.');
            console.log('Sending user through Slack authorization.');

            reply('Getting authorization code...').redirect(oauth.getAuthCode(config.appCredentials.dev.slack));
        } else if (request.query.code !== undefined) {
            console.log('Authorization code detected!');
            console.log('Fetching Slack access token.');

            oauth.getSlackAccessToken(request.query.code, config.appCredentials.dev.slack)
            .then((accessTokenRequestResponse) => {
                console.log(accessTokenRequestResponse);
                reply(`Got access token: ${accessTokenRequestResponse.access_token}`);
            });
        }
    }
})

// Start server
server.start((error) => {
    if (error) {
        throw error;
    }

    console.log(`Server running at: ${server.info.uri}`);
});
@@ -220,32 +220,81 @@
 t: 3000%0D%0A%7D);%0D%0A%0D%0A
+let fitbitAccessToken;%0D%0Alet slackAccessToken;%0D%0A%0D%0A
 server.route(%7B%0D%0A
@@ -935,16 +935,18 @@
 token;%0D%0A
+%0D%0A
         
@@ -993,32 +993,80 @@
 ccessToken%7D%60);%0D%0A
+            reply('Got Fitbit access token!');%0D%0A
         %7D%0D%0A    %7D
@@ -1963,16 +1963,22 @@
 ly(%60Got 
+Slack 
 access t

Add variables for access tokens