{"size": 1062, "ext": "js", "max_stars_repo_path": "src/store/session/actions.js", "max_stars_repo_name": "mstaack/quasar-cloud-demo", "max_stars_repo_stars_event_min_datetime": "2018-05-10T14:02:21.000Z", "max_stars_repo_stars_event_max_datetime": "2021-12-03T22:40:14.000Z", "max_issues_repo_path": "src/store/session/actions.js", "max_issues_repo_name": "mstaack/quasar-cloud-demo", "max_issues_repo_issues_event_min_datetime": "2018-05-14T15:39:44.000Z", "max_issues_repo_issues_event_max_datetime": "2019-09-12T02:39:23.000Z", "max_forks_repo_path": "src/store/session/actions.js", "max_forks_repo_name": "mstaack/quasar-cloud-demo", "max_forks_repo_forks_event_min_datetime": "2018-05-14T15:30:26.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-22T19:56:09.000Z"}
{"max_stars_count": 52, "max_issues_count": 4, "max_forks_count": 28, "avg_line_length": 21.6734693878, "max_line_length": 59, "alphanum_fraction": 0.6365348399}
import axios from 'axios'

import {LocalStorage} from 'quasar'
import setAxiosHeaders from './helpers'

export function init ({state}) {
  setAxiosHeaders(state)
}

export function register ({commit, state}, form) {
  return axios.post('auth/register', form)
    .then(response => {
      commit('LOGIN', response.data.user)
      setAxiosHeaders(state)
    })
}

export function login ({commit, dispatch, getters}, form) {

  if (getters.isAuthenticated) return dispatch('validate')

  return axios.post('auth/login', form)
    .then(response => {
      const user = response.data.user
      commit('LOGIN', user)
      return user
    })
}

export function validate ({commit, state}) {
  if (!state.user) return Promise.resolve(null)

  return axios.get('auth/user')
    .then(response => {
      const user = response.data.user
      commit('LOGIN', user)
      return user
    }).catch(error => {
      if (error.response.status === 401) {
        commit('LOGOUT')
      }
      return null
    })
}

export function logout ({commit}) {
  commit('LOGOUT')
}
