// 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;
          const { id, firstname, lastname } = athlete;
          return { token: access_token, athleteID: id, athleteDetails: { firstname, lastname } };
        },
          error => ({ error }),
      );
};

@@ -569,16 +569,17 @@
 a,%0A  %7D;%0A
+%0A
   return
@@ -604,18 +604,16 @@
 st)%0A    
-  
 .then(%0A 
@@ -621,19 +621,16 @@
     
-  
-(
 response
 ) =%3E
@@ -629,33 +629,36 @@
 onse
-)
  =%3E 
-%7B%0A          const %7B 
+(%7B token: response.data.
 acce
@@ -666,24 +666,32 @@
 s_token,
+%0A       
  athlete
  %7D = res
@@ -682,20 +682,19 @@
  athlete
- %7D =
+ID:
  respons
@@ -703,178 +703,158 @@
 data
-;%0A          const %7B id, firstname, lastname %7D = athlete;%0A          return %7B token: access_token, athleteID: id, athleteDetails: %7B firstname, lastname %7D %7D;%0A        %7D,%0A    
+.athlete.id,%0A        athleteDetails: %7B%0A          firstname: response.data.athlete.firstname,%0A          lastname: response.data.athlete.lastname %7D %7D),%0A
     
@@ -873,24 +873,22 @@
 %7B error %7D),%0A
-  
     );%0A%7D;%0A

format response.