blob: c42b9da148963211b2891de662445bb2fc201155 [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,
Yoshie Muranakad624dae2020-09-21 14:35:58 -07009 xsrfCookie: Cookies.get('XSRF-TOKEN'),
10 isAuthenticatedCookie: Cookies.get('IsAuthenticated')
Derick Montaguee080a1a2019-12-04 16:30:08 -060011 },
12 getters: {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080013 authError: state => state.authError,
Yoshie Muranakad624dae2020-09-21 14:35:58 -070014 isLoggedIn: state => {
15 return (
16 state.xsrfCookie !== undefined || state.isAuthenticatedCookie == 'true'
17 );
18 },
19 token: state => state.xsrfCookie
Derick Montaguee080a1a2019-12-04 16:30:08 -060020 },
21 mutations: {
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080022 authSuccess(state) {
Yoshie Muranaka4b0fc1d2020-01-06 07:36:16 -080023 state.authError = false;
Yoshie Muranakad624dae2020-09-21 14:35:58 -070024 state.xsrfCookie = Cookies.get('XSRF-TOKEN');
Derick Montaguee080a1a2019-12-04 16:30:08 -060025 },
Derick Montaguea06fe462020-03-11 13:48:42 -050026 authError(state, authError = true) {
27 state.authError = authError;
Derick Montague676f2fc2019-12-23 20:53:49 -060028 },
Yoshie Muranakad624dae2020-09-21 14:35:58 -070029 logout(state) {
Derick Montaguefded0d12019-12-11 06:16:40 -060030 Cookies.remove('XSRF-TOKEN');
Yoshie Muranakad624dae2020-09-21 14:35:58 -070031 Cookies.remove('IsAuthenticated');
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053032 localStorage.removeItem('storedUsername');
Yoshie Muranakad624dae2020-09-21 14:35:58 -070033 state.xsrfCookie = undefined;
34 state.isAuthenticatedCookie = undefined;
Derick Montaguee080a1a2019-12-04 16:30:08 -060035 }
36 },
37 actions: {
Yoshie Muranakad624dae2020-09-21 14:35:58 -070038 login({ commit }, { username, password }) {
Derick Montaguea06fe462020-03-11 13:48:42 -050039 commit('authError', false);
Derick Montaguee080a1a2019-12-04 16:30:08 -060040 return api
Yoshie Muranakad624dae2020-09-21 14:35:58 -070041 .post('/login', { data: [username, password] })
Derick Montaguefded0d12019-12-11 06:16:40 -060042 .then(() => commit('authSuccess'))
Derick Montaguee080a1a2019-12-04 16:30:08 -060043 .catch(error => {
Derick Montaguefded0d12019-12-11 06:16:40 -060044 commit('authError');
Derick Montaguee080a1a2019-12-04 16:30:08 -060045 throw new Error(error);
46 });
47 },
48 logout({ commit }) {
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080049 api
Derick Montaguefded0d12019-12-11 06:16:40 -060050 .post('/logout', { data: [] })
51 .then(() => commit('logout'))
Derick Montaguec031b692020-02-12 15:55:42 -060052 .then(() => router.go('/login'))
Yoshie Muranaka6ce1a072019-12-06 14:13:59 -080053 .catch(error => console.log(error));
Yoshie Muranaka2c98b092020-06-22 13:28:09 -070054 },
55 async checkPasswordChangeRequired(_, username) {
56 return await api
57 .get(`/redfish/v1/AccountService/Accounts/${username}`)
58 .then(({ data: { PasswordChangeRequired } }) => PasswordChangeRequired)
59 .catch(error => console.log(error));
Yoshie Muranakad624dae2020-09-21 14:35:58 -070060 },
61 resetStoreState({ state }) {
62 state.authError = false;
63 state.xsrfCookie = Cookies.get('XSRF-TOKEN');
64 state.isAuthenticatedCookie = Cookies.get('IsAuthenticated');
Derick Montaguee080a1a2019-12-04 16:30:08 -060065 }
66 }
67};
68
69export default AuthenticationStore;