summaryrefslogtreecommitdiff
path: root/src/store/modules/Authentication/AuthenticanStore.js
blob: 0dd616a9f830bef666b1a04bb0f62f8d0af91dd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import api from '@/store/api';
import Cookies from 'js-cookie';
import router from '@/router';

const AuthenticationStore = {
  namespaced: true,
  state: {
    authError: false,
    cookie: Cookies.get('XSRF-TOKEN')
  },
  getters: {
    authError: state => state.authError,
    isLoggedIn: state => !!state.cookie,
    token: state => state.cookie
  },
  mutations: {
    authSuccess(state) {
      state.authError = false;
      state.cookie = Cookies.get('XSRF-TOKEN');
    },
    authError(state, authError = true) {
      state.authError = authError;
    },
    logout() {
      Cookies.remove('XSRF-TOKEN');
      localStorage.removeItem('storedUsername');
    }
  },
  actions: {
    login({ commit }, auth) {
      commit('authError', false);
      return api
        .post('/login', { data: auth })
        .then(() => commit('authSuccess'))
        .catch(error => {
          commit('authError');
          throw new Error(error);
        });
    },
    logout({ commit }) {
      api
        .post('/logout', { data: [] })
        .then(() => commit('logout'))
        .then(() => router.go('/login'))
        .catch(error => console.log(error));
    },
    async checkPasswordChangeRequired(_, username) {
      return await api
        .get(`/redfish/v1/AccountService/Accounts/${username}`)
        .then(({ data: { PasswordChangeRequired } }) => PasswordChangeRequired)
        .catch(error => console.log(error));
    }
  }
};

export default AuthenticationStore;