summaryrefslogtreecommitdiff
path: root/src/store
diff options
context:
space:
mode:
Diffstat (limited to 'src/store')
-rw-r--r--src/store/api.js15
-rw-r--r--src/store/index.js4
-rw-r--r--src/store/modules/Authentication/AuthenticanStore.js56
3 files changed, 64 insertions, 11 deletions
diff --git a/src/store/api.js b/src/store/api.js
index d40ad0ad..39a6355c 100644
--- a/src/store/api.js
+++ b/src/store/api.js
@@ -2,15 +2,9 @@ import Axios from "axios";
const api = Axios.create();
-// TODO: this is a temporary workaround until
-// authentication with login is working
-const username = process.env.VUE_APP_USERNAME;
-const password = process.env.VUE_APP_PASSWORD;
-if (username && password) {
- api.defaults.auth = {};
- api.defaults.auth.username = username;
- api.defaults.auth.password = password;
-}
+// TODO: Permanent authentication solution
+// Using defaults to set auth for sending
+// auth object in header
export default {
get(path) {
@@ -30,5 +24,6 @@ export default {
},
all(promises) {
return Axios.all(promises);
- }
+ },
+ defaults: api.defaults
};
diff --git a/src/store/index.js b/src/store/index.js
index af06a471..b4be6ccd 100644
--- a/src/store/index.js
+++ b/src/store/index.js
@@ -1,8 +1,9 @@
import Vue from "vue";
import Vuex from "vuex";
-import LocalUserManagementStore from "./modules/AccessControl/LocalUserMangementStore";
import GlobalStore from "./modules/GlobalStore";
+import AuthenticationStore from "./modules/Authentication/AuthenticanStore";
+import LocalUserManagementStore from "./modules/AccessControl/LocalUserMangementStore";
Vue.use(Vuex);
@@ -12,6 +13,7 @@ export default new Vuex.Store({
actions: {},
modules: {
global: GlobalStore,
+ authentication: AuthenticationStore,
localUsers: LocalUserManagementStore
}
});
diff --git a/src/store/modules/Authentication/AuthenticanStore.js b/src/store/modules/Authentication/AuthenticanStore.js
new file mode 100644
index 00000000..828c3cc8
--- /dev/null
+++ b/src/store/modules/Authentication/AuthenticanStore.js
@@ -0,0 +1,56 @@
+import api from "../../api";
+
+const AuthenticationStore = {
+ namespaced: true,
+ state: {
+ auth: {},
+ status: "",
+ token: sessionStorage.getItem("token") || ""
+ },
+ getters: {
+ authStatus: state => state.status,
+ isLoggedIn: state => !!state.token
+ },
+ mutations: {
+ authRequest(state) {
+ state.status = "loading";
+ },
+ authSuccess(state, token, auth) {
+ state.status = "authenicated";
+ state.auth = auth;
+ state.token = token;
+ },
+ authError(state) {
+ state.status = "error";
+ },
+ logout(state) {
+ state.status = "";
+ state.token = "";
+ }
+ },
+ actions: {
+ login({ commit }, auth) {
+ commit("authRequest");
+ return api
+ .post("/login", auth)
+ .then(response => {
+ const token = response.data.token;
+ sessionStorage.setItem("token", token);
+ api.defaults.auth = auth; // TODO Permanent Solution
+ commit("authSuccess", token, auth);
+ })
+ .catch(error => {
+ commit("authError");
+ sessionStorage.removeItem("token");
+ throw new Error(error);
+ });
+ },
+ logout({ commit }) {
+ commit("logout");
+ sessionStorage.removeItem("token");
+ api.defaults.auth = {}; // Permanent solution
+ }
+ }
+};
+
+export default AuthenticationStore;