blob: 07d5ee8b1a09841f889155f6ed072c54d4ce0ecb [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'),
Derick Montague602e98a2020-10-21 16:20:00 -050010 isAuthenticatedCookie: Cookies.get('IsAuthenticated'),
Derick Montaguee080a1a2019-12-04 16:30:08 -060011 },
12 getters: {
Derick Montague602e98a2020-10-21 16:20:00 -050013 authError: (state) => state.authError,
14 isLoggedIn: (state) => {
Yoshie Muranakad624dae2020-09-21 14:35:58 -070015 return (
16 state.xsrfCookie !== undefined || state.isAuthenticatedCookie == 'true'
17 );
18 },
Derick Montague602e98a2020-10-21 16:20:00 -050019 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 Montague602e98a2020-10-21 16:20:00 -050035 },
Derick Montaguee080a1a2019-12-04 16:30:08 -060036 },
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 Montague602e98a2020-10-21 16:20:00 -050043 .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'))
Derick Montague602e98a2020-10-21 16:20:00 -050053 .catch((error) => console.log(error));
Yoshie Muranaka2c98b092020-06-22 13:28:09 -070054 },
Lei YUa5fefd02021-08-06 17:12:29 +080055 checkPasswordChangeRequired(_, username) {
56 api
Yoshie Muranaka2c98b092020-06-22 13:28:09 -070057 .get(`/redfish/v1/AccountService/Accounts/${username}`)
58 .then(({ data: { PasswordChangeRequired } }) => PasswordChangeRequired)
Derick Montague602e98a2020-10-21 16:20:00 -050059 .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 Montague602e98a2020-10-21 16:20:00 -050065 },
66 },
Derick Montaguee080a1a2019-12-04 16:30:08 -060067};
68
69export default AuthenticationStore;