// constants
import { API_ENDPOINT, RESOURCES, METHODS } from '../constants/rest';

// helper
import { callJSONApi } from './helpers/api';


export const authenticate = (temporaryAccessToken) => {
  // TODO: use config file to retrieve client id + client secret.
  const formData = new FormData();
  formData.append('client_id', '15688');
  formData.append('client_secret', '');
  formData.append('code', temporaryAccessToken);

  const request = {
    endpoint: {
      url: API_ENDPOINT + RESOURCES.OAUTH,
      httpVerb: METHODS.POST,
    },
    parameters: formData,
  };
  return callJSONApi(request)
      .then(
        (response) => {
          const { access_token, athlete } = response.data;
          return { access_token, athlete };
        },
          error => ({ error }),
      );
};

@@ -695,16 +695,71 @@
 e.data;%0A
+          const %7B id, firstname, lastname %7D = athlete;%0A
         
@@ -768,16 +768,23 @@
 return %7B
+ token:
  access_
@@ -797,16 +797,63 @@
  athlete
+ID: id, athleteDetails: %7B firstname, lastname %7D
  %7D;%0A    

format returned data.