diff options
author | Mateja <mail@matejamaric.com> | 2021-07-29 02:46:33 +0200 |
---|---|---|
committer | Mateja <mail@matejamaric.com> | 2021-07-29 02:46:33 +0200 |
commit | 015d67cf738e4ad6d397824dc09a44d85d643b75 (patch) | |
tree | 122c50c85b5659332117b735790231b257b4724a /client/src/store | |
parent | 9f4a1c17d4f544784dc5e11ecf6d04c8b5d0582a (diff) | |
download | mevn-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.js | 44 |
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: { |