This commit is contained in:
99
src/store/auth.js
Normal file
99
src/store/auth.js
Normal file
@@ -0,0 +1,99 @@
|
||||
import Vue from 'vue'
|
||||
import Vuex from 'vuex'
|
||||
import axios from 'axios'
|
||||
|
||||
const JWT_KEY_NAME = "JWT";
|
||||
const baseURL = process.env.VUE_APP_API_LOCATION;
|
||||
|
||||
Vue.use(Vuex)
|
||||
|
||||
export default {
|
||||
state() {
|
||||
return {
|
||||
auth: {
|
||||
processing: false,
|
||||
token: localStorage.getItem(JWT_KEY_NAME) || '',
|
||||
name: ''
|
||||
}
|
||||
}
|
||||
},
|
||||
mutations: {
|
||||
auth_started(state) {
|
||||
state.auth.processing = true;
|
||||
},
|
||||
auth_success(state, token, name) {
|
||||
state.auth.processing = false;
|
||||
state.auth.token = token;
|
||||
state.auth.name = name;
|
||||
},
|
||||
auth_fail(state) {
|
||||
state.auth.processing = false;
|
||||
state.auth.token = '';
|
||||
state.auth.name = '';
|
||||
},
|
||||
logout(state) {
|
||||
state.auth.token = '';
|
||||
state.auth.name = '';
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
performLogin({commit}, creds) {
|
||||
return new Promise((resolve, reject) => {
|
||||
commit('auth_started')
|
||||
axios.post("auth/login", creds, {baseURL}).then(resp => {
|
||||
const token = resp.data.token;
|
||||
|
||||
if (!token) {
|
||||
return reject();
|
||||
}
|
||||
|
||||
const user = creds.name;
|
||||
localStorage.setItem(JWT_KEY_NAME, token)
|
||||
commit('auth_success', token, user)
|
||||
return resolve(resp);
|
||||
}).catch(err => {
|
||||
commit('auth_fail')
|
||||
localStorage.removeItem(JWT_KEY_NAME)
|
||||
return reject(err);
|
||||
})
|
||||
})
|
||||
},
|
||||
performRegister({commit}, creds) {
|
||||
return new Promise((resolve, reject) => {
|
||||
commit('auth_started')
|
||||
axios.post("auth/signup", creds, {baseURL}).then(resp => {
|
||||
const token = resp.data.token;
|
||||
|
||||
if (!token) {
|
||||
return reject();
|
||||
}
|
||||
|
||||
const user = creds.name;
|
||||
localStorage.setItem(JWT_KEY_NAME, token)
|
||||
commit('auth_success', token, user)
|
||||
resolve(resp)
|
||||
}).catch(err => {
|
||||
commit('auth_fail')
|
||||
localStorage.removeItem(JWT_KEY_NAME)
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
},
|
||||
performLogout({commit}) {
|
||||
return new Promise((resolve) => {
|
||||
localStorage.removeItem(JWT_KEY_NAME)
|
||||
commit('logout')
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
},
|
||||
modules: {},
|
||||
getters: {
|
||||
isLoggedIn(state) {
|
||||
return !!state.auth.token;
|
||||
},
|
||||
authInProgress(state) {
|
||||
return state.auth.processing;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,24 @@
|
||||
import Vue from 'vue'
|
||||
import Vuex from 'vuex'
|
||||
|
||||
import auth from "@/store/auth";
|
||||
|
||||
Vue.use(Vuex)
|
||||
|
||||
export default new Vuex.Store({
|
||||
state: {
|
||||
},
|
||||
mutations: {
|
||||
},
|
||||
actions: {
|
||||
},
|
||||
modules: {
|
||||
}
|
||||
state: {
|
||||
|
||||
},
|
||||
mutations: {
|
||||
|
||||
},
|
||||
actions: {
|
||||
|
||||
},
|
||||
modules: {
|
||||
auth
|
||||
},
|
||||
getters: {
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user