{"size": 1306, "ext": "js", "max_stars_repo_path": "web/src/store/modules/auth.js", "max_stars_repo_name": "corcronje/laravel-vue", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "web/src/store/modules/auth.js", "max_issues_repo_name": "corcronje/laravel-vue", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "web/src/store/modules/auth.js", "max_forks_repo_name": "corcronje/laravel-vue", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.5172413793, "max_line_length": 61, "alphanum_fraction": 0.6125574273}
import axios from "axios";

const state = {
  user: null,
};

const getters = {
  user: (state) => state.user,
  isAuthenticated: (state) => !!state.user,
};

const actions = {
  async register({ commit }, credentials) {
    let response = await axios.post("register", credentials);
    commit("SET_USER", response.data.user);
    localStorage.setItem("token", response.data.user.token);
  },
  async login({ commit }, credentials) {
    let response = await axios.post("login", credentials);
    commit("SET_USER", response.data.user);
    localStorage.setItem("token", response.data.user.token);
  },
  async logout({ commit }) {
    await axios.post("logout", null, {
      headers: {
        Authorization: "Bearer " + this.getters.user.token,
      },
    });
    commit("SET_USER", null);
    localStorage.removeItem("token");
  },
  async loadUser({ commit }) {
    let response = await axios.get("user", {
      headers: {
        Authorization: "Bearer " + this.getters.user.token,
      },
    });
    commit("SET_USER", {
      name: response.data.user.name,
      email: response.data.user.email,
      token: this.getters.user.token,
    });
  },
};

const mutations = {
  SET_USER(state, user) {
    state.user = user;
  },
};

export default {
  state,
  getters,
  actions,
  mutations,
};
