Add login and logout functionality
- Add AuthenticationStore
- Add ability to login and logout
- Add route navigation guard
- Add login styles
- Add temporary authentication for api call
- Add Login directory
- Add index.js
In order to login a .env.development.local file that contains
BASE_URL="https://<ip address> or <FQDN>"
Signed-off-by: Derick Montague <derick.montague@ibm.com>
Change-Id: I88b93e287e66f4bae82a1ec2934cdef12d78264e
diff --git a/src/store/modules/Authentication/AuthenticanStore.js b/src/store/modules/Authentication/AuthenticanStore.js
new file mode 100644
index 0000000..828c3cc
--- /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;