From d1e7d7bf27e25405b6fcdd485e5aa1a3aeb46265 Mon Sep 17 00:00:00 2001 From: jdlugosz963 Date: Tue, 9 Nov 2021 22:24:46 +0100 Subject: Add login system --- src/App.vue | 11 +++++++ src/assets/logo.png | Bin 0 -> 6849 bytes src/components/HelloWorld.vue | 60 ++++++++++++++++++++++++++++++++++++ src/main.js | 15 +++++++++ src/router/index.js | 31 +++++++++++++++++++ src/services/AuthService.js | 17 ++++++++++ src/store/index.js | 48 +++++++++++++++++++++++++++++ src/views/Home.vue | 30 ++++++++++++++++++ src/views/Login.vue | 56 +++++++++++++++++++++++++++++++++ src/views/Register.vue | 70 ++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 338 insertions(+) create mode 100644 src/App.vue create mode 100644 src/assets/logo.png create mode 100644 src/components/HelloWorld.vue create mode 100644 src/main.js create mode 100644 src/router/index.js create mode 100644 src/services/AuthService.js create mode 100644 src/store/index.js create mode 100644 src/views/Home.vue create mode 100644 src/views/Login.vue create mode 100644 src/views/Register.vue (limited to 'src') diff --git a/src/App.vue b/src/App.vue new file mode 100644 index 0000000..403eae2 --- /dev/null +++ b/src/App.vue @@ -0,0 +1,11 @@ + + + + diff --git a/src/assets/logo.png b/src/assets/logo.png new file mode 100644 index 0000000..f3d2503 Binary files /dev/null and b/src/assets/logo.png differ diff --git a/src/components/HelloWorld.vue b/src/components/HelloWorld.vue new file mode 100644 index 0000000..1c544cb --- /dev/null +++ b/src/components/HelloWorld.vue @@ -0,0 +1,60 @@ + + + + + + diff --git a/src/main.js b/src/main.js new file mode 100644 index 0000000..c370aef --- /dev/null +++ b/src/main.js @@ -0,0 +1,15 @@ +import Vue from 'vue' +import App from './App.vue' +import router from './router' +import store from './store' +import Axios from 'axios' + +Vue.config.productionTip = false + +Axios.defaults.headers.common['Authorization'] = store.state.token !== "" && `Token ${store.state.token}`; + +new Vue({ + router, + store, + render: h => h(App) +}).$mount('#app') diff --git a/src/router/index.js b/src/router/index.js new file mode 100644 index 0000000..c67b61d --- /dev/null +++ b/src/router/index.js @@ -0,0 +1,31 @@ +import Vue from 'vue' +import VueRouter from 'vue-router' +import Home from '../views/Home.vue' +import Register from '../views/Register.vue' +import Login from '../views/Login.vue' + +Vue.use(VueRouter) + +const routes = [ + { + path: '/', + name: 'Home', + component: Home + }, + { + path: '/register', + name: "register", + component: Register + }, + { + path: '/login', + name: "login", + component: Login + } +] + +const router = new VueRouter({ + routes +}) + +export default router diff --git a/src/services/AuthService.js b/src/services/AuthService.js new file mode 100644 index 0000000..626a8fb --- /dev/null +++ b/src/services/AuthService.js @@ -0,0 +1,17 @@ +import axios from "axios"; + +const url = 'http://localhost:8000/api/auth/' + +export default { + async login(data) { + return await axios + .post(url+'login/', data) + .then(res => res.data) + }, + + async register(data) { + return await axios + .post(url+'register/', data) + .then(res => res.data) + } +} \ No newline at end of file diff --git a/src/store/index.js b/src/store/index.js new file mode 100644 index 0000000..fa8a1bd --- /dev/null +++ b/src/store/index.js @@ -0,0 +1,48 @@ +import Vue from 'vue' +import Vuex from 'vuex' +import Axios from 'axios' +import createPersistedState from "vuex-persistedstate"; + +Vue.use(Vuex) + +const get_default_state = () => { + return { + token: "", + user: {} + } +} + +const store = new Vuex.Store({ + plugins: [createPersistedState()], + state: get_default_state(), + + mutations: { + set_token: (state, token) => state.token = token, + set_user: (state, user) => state.user = user, + reset_state: (state) => Object.assign(state, get_default_state()) + }, + + getters: { + is_logged_in: (state) => { + return state.token !== "" + }, + + get_user: (state) => { + return state.user + } + }, + + actions: { + login: ({commit}, {token, user}) => { + commit('set_token', token) + commit('set_user', user) + + Axios.defaults.headers.common['Authorization'] = `Token ${token}`; + }, + logout: ({commit}) => { + commit('reset_state') + } + } +}) + +export default store diff --git a/src/views/Home.vue b/src/views/Home.vue new file mode 100644 index 0000000..e5b446b --- /dev/null +++ b/src/views/Home.vue @@ -0,0 +1,30 @@ + + + diff --git a/src/views/Login.vue b/src/views/Login.vue new file mode 100644 index 0000000..ae43ecd --- /dev/null +++ b/src/views/Login.vue @@ -0,0 +1,56 @@ + + + + + \ No newline at end of file diff --git a/src/views/Register.vue b/src/views/Register.vue new file mode 100644 index 0000000..c03f5fd --- /dev/null +++ b/src/views/Register.vue @@ -0,0 +1,70 @@ + + + + + \ No newline at end of file -- cgit v1.2.3