const URL_LOGIN =
  "/one-api/v1/system/auth/login";
const URL_IS_LOGIN =
  "/one-api/v1/system/auth/isLogin";

const ONE_TOKEN = "oneTokenId";

export async function login(userName, userPassword) {
  try {
    var resp = await axios.post(URL_LOGIN, {
      userName: userName,
      userPassword: userPassword
    });
    if (resp.status != 200) {
      return { status: "ERR", message: resp.statusText };
    }
    let data = resp.data;
    //Hardcoded 1st
    data.nextPage = "/one-ui/apps/main/";
    return data;
  } catch (e) {
    return { status: "ERR", message: e.message };
  }
}

export async function isLogin(token) {
  try {
    var resp = await axios.post(URL_IS_LOGIN, {
      token: token
    });
    if (resp.status != 200) {
      return { status: "ERR", message: resp.statusText };
    }
    let data = resp.data;
    //Hardcoded 1st
    data.nextPage = "/one-ui/apps/main/";
    return data;
  } catch (e) {
    return { status: "ERR", message: e.message };
  }
}

export function loadOneToken() {
  return localStorage.getItem(ONE_TOKEN);
}
export function saveOneToken(token) {
  localStorage.setItem(ONE_TOKEN, token);
}
export function removeOneToken() {
  localStorage.removeItem(ONE_TOKEN);
}
