import { createStore } from 'vuex'

export const store = createStore({
  state: {
    user: null,
  },
  mutations: {
    set_user(state, user) {
      state.user = user;
    },
  },
  actions: {
    async check_user({ commit }) {
      let u = await fetch("/is_authenticated/", { Accept: "application/json" });
      let j = await u.json();
      if (j.user === null) {
        window.location = "/login/?next=/frontend/";
      } else {
        commit("set_user", j.user);
      }
    },
  },
});
