added login stuff
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-12-06 01:43:47 +01:00
parent 0a09a1b0b7
commit 633f2af5eb
11 changed files with 255 additions and 33 deletions

99
src/store/auth.js Normal file
View 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;
}
}
}

View File

@@ -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: {
}
})