import { action, atom } from "nanostores";
import { api } from "../api";

export const userInfoLoading$ = atom(true);

export const isAuthorized$ = atom(false);

export const setAuth = action(isAuthorized$, "setAuth", (store, newValue) => {
  store.set(newValue);
});

async function checkAuth() {
  try {
    await api.get("/accounts/profile/");

    setAuth(true);
  } catch (e) {
    console.log("Not authorized");
  } finally {
    userInfoLoading$.set(false);
  }
}

checkAuth();
