blob: 0dd616a9f830bef666b1a04bb0f62f8d0af91dd2 [file] [log] [blame]
SurenNeware61859092020-10-01 09:37:32 +05301import api from '@/store/api';
Derick Montaguefded0d12019-12-11 06:16:40 -06002import Cookies from 'js-cookie';
SurenNeware61859092020-10-01 09:37:32 +05303import router from '@/router';
Derick Montaguee080a1a2019-12-04 16:30:08 -06004
5const AuthenticationStore = {
6 namespaced: true,
7 state: {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -08008 authError: false,
Derick Montaguefded0d12019-12-11 06:16:40 -06009 cookie: Cookies.get('XSRF-TOKEN')
Derick Montaguee080a1a2019-12-04 16:30:08 -060010 },
11 getters: {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080012 authError: state => state.authError,
Yoshie Muranaka23f227d2020-05-01 14:19:43 -070013 isLoggedIn: state => !!state.cookie,
14 token: state => state.cookie
Derick Montaguee080a1a2019-12-04 16:30:08 -060015 },
16 mutations: {
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080017 authSuccess(state) {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080018 state.authError = false;
Derick Montaguefded0d12019-12-11 06:16:40 -060019 state.cookie = Cookies.get('XSRF-TOKEN');
Derick Montaguee080a1a2019-12-04 16:30:08 -060020 },
Derick Montaguea06fe462020-03-11 13:48:42 -050021 authError(state, authError = true) {
22 state.authError = authError;
Derick Montague676f2fc2019-12-23 20:53:49 -060023 },
Derick Montaguec031b692020-02-12 15:55:42 -060024 logout() {
Derick Montaguefded0d12019-12-11 06:16:40 -060025 Cookies.remove('XSRF-TOKEN');
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053026 localStorage.removeItem('storedUsername');
Derick Montaguee080a1a2019-12-04 16:30:08 -060027 }
28 },
29 actions: {
30 login({ commit }, auth) {
Derick Montaguea06fe462020-03-11 13:48:42 -050031 commit('authError', false);
Derick Montaguee080a1a2019-12-04 16:30:08 -060032 return api
Derick Montaguefded0d12019-12-11 06:16:40 -060033 .post('/login', { data: auth })
34 .then(() => commit('authSuccess'))
Derick Montaguee080a1a2019-12-04 16:30:08 -060035 .catch(error => {
Derick Montaguefded0d12019-12-11 06:16:40 -060036 commit('authError');
Derick Montaguee080a1a2019-12-04 16:30:08 -060037 throw new Error(error);
38 });
39 },
40 logout({ commit }) {
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080041 api
Derick Montaguefded0d12019-12-11 06:16:40 -060042 .post('/logout', { data: [] })
43 .then(() => commit('logout'))
Derick Montaguec031b692020-02-12 15:55:42 -060044 .then(() => router.go('/login'))
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080045 .catch(error => console.log(error));
Yoshie Muranaka2c98b092020-06-22 13:28:09 -070046 },
47 async checkPasswordChangeRequired(_, username) {
48 return await api
49 .get(`/redfish/v1/AccountService/Accounts/${username}`)
50 .then(({ data: { PasswordChangeRequired } }) => PasswordChangeRequired)
51 .catch(error => console.log(error));
Derick Montaguee080a1a2019-12-04 16:30:08 -060052 }
53 }
54};
55
56export default AuthenticationStore;