{"size": 1082, "ext": "js", "max_stars_repo_path": "src/store.js", "max_stars_repo_name": "rekkusu/submarine-frontend", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/store.js", "max_issues_repo_name": "rekkusu/submarine-frontend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/store.js", "max_forks_repo_name": "rekkusu/submarine-frontend", "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": 20.037037037, "max_line_length": 65, "alphanum_fraction": 0.4842883549}
import axios from 'axios'
import createLogger from 'vuex/dist/logger'

export default {
  state: {
    username: "",
    token: ""
  },
  mutations: {
    setToken(state, token) {
      state.token = token;
      if (token === '') {
        return
      }

      axios.get('/api/v1/me', {
        headers: {
          'Authorization': 'Bearer ' + state.token
        }
      }).then(resp => {
        state.username = resp.data.username;
      }).catch(() => {
        state.token = "";
        state.username = "";
      });
    },
    logout(state) {
      state.token = '';
    }
  },
  plugins: [
    store => {
      // initialize
      const token = window.localStorage.getItem('token');
      if (token) {
        store.commit('setToken', token);
      }

      store.subscribe((mutation) => {
        // save
        if (mutation.type === 'setToken') {
          window.localStorage.setItem('token', mutation.payload);
        }

        // logout
        if (mutation.type === 'logout') {
          window.localStorage.removeItem('token');
        }
      });
    }
  ]
}

