import _ from "lodash";
import axios from "axios";
import { Auth } from "aws-amplify";
export const getSessionTokenAWS = async prefix => {
  const data = await Auth.currentSession();
  const token = data.accessToken.jwtToken;
  if (_.isUndefined(prefix)) return token;
  return `${prefix} ${token}`;
};

// export const getRefreshTokenAWS = async () => {
//   const data = await Auth.currentSession();
//   return data.getRefreshToken().token;
// };

export const getSessionUidAWS = () => {
  return new Promise((resolve, reject) => {
    Auth.currentSession().then(data => {
      resolve(data.accessToken.payload.username);
    }).catch(err => {
      console.log("getSessionUidAWS error", err);
      reject(err);
    });
  });
};
export const authedFunction = async request => {
  const authkey = await getSessionTokenAWS();
  if (!request.headers) request.headers = {};
  request.headers.authkey = authkey;
  return axios(request);
};

// export const LogoutAWS = () => {
//   return new Promise((resolve, reject) => {
//     Auth.signOut()
//       .then(() => {
//         resolve();
//       })
//       .catch(err => {
//         reject(err);
//       });
//   });
// };

// export const isTheBest = auth => {
//   return auth && auth.site === 'plussSpace' && auth.type === 'master';
// };

export const getApiError = error => {
  if (!__DEV__) return error;
  try {
    const errorData = error.response.data;
    return errorData || error;
  } catch (e) {
    return error;
  }
};
//# sourceMappingURL=session.js.map