resources/js/admin/store/modules/auth.js   A
last analyzed

Complexity

Total Complexity 16
Complexity/F 1.23

Size

Lines of Code 80
Function Count 13

Duplication

Duplicated Lines 80
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 44
c 0
b 0
f 0
dl 80
loc 80
rs 10
wmc 16
mnd 3
bc 3
fnc 13
bpm 0.2307
cpm 1.2306
noi 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1 View Code Duplication
import axios from 'axios'
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
import Cookies from 'js-cookie'
3
import * as types from '../mutation-types'
4
5
// state
6
export const state = {
7
  user: null,
8
  token: Cookies.get('token')
9
}
10
11
// getters
12
export const getters = {
13
  user: state => state.user,
14
  token: state => state.token,
15
  check: state => state.user !== null
16
}
17
18
// mutations
19
export const mutations = {
20
  [types.SAVE_TOKEN] (state, { token, remember }) {
21
    state.token = token
22
    Cookies.set('token', token, { expires: remember ? 365 : null })
23
  },
24
25
  [types.FETCH_USER_SUCCESS] (state, { user }) {
26
    state.user = user
27
  },
28
29
  [types.FETCH_USER_FAILURE] (state) {
30
    state.token = null
31
    Cookies.remove('token')
32
  },
33
34
  [types.LOGOUT] (state) {
35
    state.user = null
36
    state.token = null
37
38
    Cookies.remove('token')
39
  },
40
41
  [types.UPDATE_USER] (state, { user }) {
42
    state.user = user
43
  }
44
}
45
46
// actions
47
export const actions = {
48
  saveToken ({ commit, dispatch }, payload) {
49
    commit(types.SAVE_TOKEN, payload)
50
  },
51
52
  async fetchUser ({ commit }) {
53
    try {
54
      const { data } = await axios.get('/api/user')
55
56
      commit(types.FETCH_USER_SUCCESS, { user: data })
57
    } catch (e) {
58
      commit(types.FETCH_USER_FAILURE)
59
    }
60
  },
61
62
  updateUser ({ commit }, payload) {
63
    commit(types.UPDATE_USER, payload)
64
  },
65
66
  async logout ({ commit }) {
67
    try {
68
      // await axios.post('/api/logout')
69
      commit(types.FETCH_USER_SUCCESS, { user: null, token: null })
70
    } catch (e) { }
0 ignored issues
show
Coding Style Comprehensibility Best Practice introduced by
Empty catch clauses should be used with caution; consider adding a comment why this is needed.
Loading history...
71
72
    commit(types.LOGOUT)
73
  },
74
75
  async fetchOauthUrl (ctx, { provider }) {
76
    const { data } = await axios.post(`/api/oauth/${provider}`)
77
78
    return data.url
79
  }
80
}
81