/* eslint-disable no-param-reassign */

import auth from '../../utils/auth';
import {
  extractUser,
  setFlag,
  clearFlag,
  isLoggedIn,
} from '../../utils/auth-actions';
import { LoggedOutError, Auth0Error } from '../../errors';

function createDefaultState() {
  return {
    idToken: '',
    accessToken: '',
    details: {
      nickname: '',
      email: '',
      emailVerified: false,
    },
  };
}

const mutations = {
  SET_ID_TOKEN(state, idToken) {
    state.idToken = idToken;
  },

  SET_ACCESS_TOKEN(state, accessToken) {
    state.accessToken = accessToken;
  },

  SET_DETAILS(state, details) {
    state.details = details;
  },
};

const actions = {
  getUser: ({ commit }) =>
    new Promise((resolve, reject) => {
      if (isLoggedIn()) {
        auth.checkSession(
          { responseType: 'id_token token' },
          (err, authResult) => {
            const { idToken, accessToken, idTokenPayload } = authResult;

            if (err && err.error === 'login_required') {
              clearFlag();
              return reject(new LoggedOutError());
            }
            if (err || !idToken || !accessToken || !idTokenPayload) {
              clearFlag();
              return reject(new Auth0Error());
            }

            commit('SET_ID_TOKEN', idToken);
            commit('SET_ACCESS_TOKEN', accessToken);
            commit('SET_DETAILS', extractUser(idTokenPayload));
            setFlag();
            return resolve();
          }
        );
      } else {
        clearFlag();
        reject(new LoggedOutError());
      }
    }),
};

export default {
  namespaced: true,
  state: createDefaultState(),
  mutations,
  actions,
  getters: {},
};

@@ -865,82 +865,8 @@
 %3E %7B%0A
-            const %7B idToken, accessToken, idTokenPayload %7D = authResult;%0A%0A
     
@@ -1030,44 +1030,163 @@
 if (
-err %7C%7C !idToken %7C%7C !accessToken %7C%7C !
+%0A              err %7C%7C%0A              !authResult %7C%7C%0A              !authResult.idToken %7C%7C%0A              !authResult.accessToken %7C%7C%0A              !authResult.
 idTo
@@ -1195,16 +1195,29 @@
 nPayload
+%0A            
 ) %7B%0A    
@@ -1336,16 +1336,27 @@
 TOKEN', 
+authResult.
 idToken)
@@ -1396,16 +1396,27 @@
 TOKEN', 
+authResult.
 accessTo
@@ -1467,16 +1467,27 @@
 actUser(
+authResult.
 idTokenP

fix error when authResult does not exist