aboutsummaryrefslogtreecommitdiff
path: root/client/src/store
diff options
context:
space:
mode:
authorMateja <mail@matejamaric.com>2021-07-29 02:46:33 +0200
committerMateja <mail@matejamaric.com>2021-07-29 02:46:33 +0200
commit015d67cf738e4ad6d397824dc09a44d85d643b75 (patch)
tree122c50c85b5659332117b735790231b257b4724a /client/src/store
parent9f4a1c17d4f544784dc5e11ecf6d04c8b5d0582a (diff)
downloadmevn-ecommerce-015d67cf738e4ad6d397824dc09a44d85d643b75.tar.gz
mevn-ecommerce-015d67cf738e4ad6d397824dc09a44d85d643b75.zip
Fully implement client-side login system...
Diffstat (limited to 'client/src/store')
-rw-r--r--client/src/store/index.js44
1 files changed, 43 insertions, 1 deletions
diff --git a/client/src/store/index.js b/client/src/store/index.js
index ff4648f..47f8f47 100644
--- a/client/src/store/index.js
+++ b/client/src/store/index.js
@@ -5,7 +5,9 @@ export default createStore({
state: {
products: [],
currentProduct: {},
- cart: []
+ cart: [],
+ token: localStorage.getItem('token') || '',
+ isAdmin: localStorage.getItem('isAdmin') === 'true'
},
getters: {
getProducts(state) {
@@ -37,6 +39,12 @@ export default createStore({
amount = x.quantity;
});
return amount;
+ },
+ isLoggedIn(state) {
+ return !!state.token;
+ },
+ isAdmin(state) {
+ return state.isAdmin;
}
},
mutations: {
@@ -68,6 +76,20 @@ export default createStore({
},
clearCart(state) {
state.cart.length = 0;
+ },
+ auth_set(state, token, isAdmin) {
+ state.token = token;
+ state.isAdmin = isAdmin;
+ localStorage.setItem('token', token);
+ localStorage.setItem('isAdmin', isAdmin);
+ axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;
+ },
+ auth_clean(state) {
+ state.token = null;
+ state.isAdmin = false;
+ localStorage.removeItem('token');
+ localStorage.removeItem('isAdmin');
+ delete axios.defaults.headers.common['Authorization'];
}
},
actions: {
@@ -96,6 +118,26 @@ export default createStore({
.post(`${process.env.VUE_APP_ROOT_API}/transactions/capture`, {orderId})
.then(() => true)
.catch(err => console.error(err));
+ },
+ login(context, loginData) {
+ return new Promise((resolve, reject) => {
+ axios.post(`${process.env.VUE_APP_ROOT_API}/login`, loginData)
+ .then(response => {
+ context.commit('auth_set', response.data.token, response.data.isAdmin);
+ resolve(response);
+ })
+ .catch(error => reject(error));
+ });
+ },
+ register(context, registerData) {
+ return new Promise((resolve, reject) => {
+ axios.post(`${process.env.VUE_APP_ROOT_API}/register`, registerData)
+ .then(response => {
+ context.commit('auth_set', response.data.token, response.data.isAdmin);
+ resolve(response);
+ })
+ .catch(error => reject(error));
+ });
}
},
modules: {