summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/App.vue11
-rw-r--r--src/assets/logo.pngbin0 -> 6849 bytes
-rw-r--r--src/components/HelloWorld.vue60
-rw-r--r--src/main.js15
-rw-r--r--src/router/index.js31
-rw-r--r--src/services/AuthService.js17
-rw-r--r--src/store/index.js48
-rw-r--r--src/views/Home.vue30
-rw-r--r--src/views/Login.vue56
-rw-r--r--src/views/Register.vue70
10 files changed, 338 insertions, 0 deletions
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 @@
1<template>
2 <div id="app">
3 <router-view/>
4 </div>
5</template>
6
7<script>
8export default {
9}
10</script>
11
diff --git a/src/assets/logo.png b/src/assets/logo.png
new file mode 100644
index 0000000..f3d2503
--- /dev/null
+++ b/src/assets/logo.png
Binary files 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 @@
1<template>
2 <div class="hello">
3 <h1>{{ msg }}</h1>
4 <p>
5 For a guide and recipes on how to configure / customize this project,<br>
6 check out the
7 <a href="https://cli.vuejs.org" target="_blank" rel="noopener">vue-cli documentation</a>.
8 </p>
9 <h3>Installed CLI Plugins</h3>
10 <ul>
11 <li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-babel" target="_blank" rel="noopener">babel</a></li>
12 <li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-router" target="_blank" rel="noopener">router</a></li>
13 <li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-vuex" target="_blank" rel="noopener">vuex</a></li>
14 <li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-eslint" target="_blank" rel="noopener">eslint</a></li>
15 </ul>
16 <h3>Essential Links</h3>
17 <ul>
18 <li><a href="https://vuejs.org" target="_blank" rel="noopener">Core Docs</a></li>
19 <li><a href="https://forum.vuejs.org" target="_blank" rel="noopener">Forum</a></li>
20 <li><a href="https://chat.vuejs.org" target="_blank" rel="noopener">Community Chat</a></li>
21 <li><a href="https://twitter.com/vuejs" target="_blank" rel="noopener">Twitter</a></li>
22 <li><a href="https://news.vuejs.org" target="_blank" rel="noopener">News</a></li>
23 </ul>
24 <h3>Ecosystem</h3>
25 <ul>
26 <li><a href="https://router.vuejs.org" target="_blank" rel="noopener">vue-router</a></li>
27 <li><a href="https://vuex.vuejs.org" target="_blank" rel="noopener">vuex</a></li>
28 <li><a href="https://github.com/vuejs/vue-devtools#vue-devtools" target="_blank" rel="noopener">vue-devtools</a></li>
29 <li><a href="https://vue-loader.vuejs.org" target="_blank" rel="noopener">vue-loader</a></li>
30 <li><a href="https://github.com/vuejs/awesome-vue" target="_blank" rel="noopener">awesome-vue</a></li>
31 </ul>
32 </div>
33</template>
34
35<script>
36export default {
37 name: 'HelloWorld',
38 props: {
39 msg: String
40 }
41}
42</script>
43
44<!-- Add "scoped" attribute to limit CSS to this component only -->
45<style scoped>
46h3 {
47 margin: 40px 0 0;
48}
49ul {
50 list-style-type: none;
51 padding: 0;
52}
53li {
54 display: inline-block;
55 margin: 0 10px;
56}
57a {
58 color: #42b983;
59}
60</style>
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 @@
1import Vue from 'vue'
2import App from './App.vue'
3import router from './router'
4import store from './store'
5import Axios from 'axios'
6
7Vue.config.productionTip = false
8
9Axios.defaults.headers.common['Authorization'] = store.state.token !== "" && `Token ${store.state.token}`;
10
11new Vue({
12 router,
13 store,
14 render: h => h(App)
15}).$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 @@
1import Vue from 'vue'
2import VueRouter from 'vue-router'
3import Home from '../views/Home.vue'
4import Register from '../views/Register.vue'
5import Login from '../views/Login.vue'
6
7Vue.use(VueRouter)
8
9const routes = [
10 {
11 path: '/',
12 name: 'Home',
13 component: Home
14 },
15 {
16 path: '/register',
17 name: "register",
18 component: Register
19 },
20 {
21 path: '/login',
22 name: "login",
23 component: Login
24 }
25]
26
27const router = new VueRouter({
28 routes
29})
30
31export 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 @@
1import axios from "axios";
2
3const url = 'http://localhost:8000/api/auth/'
4
5export default {
6 async login(data) {
7 return await axios
8 .post(url+'login/', data)
9 .then(res => res.data)
10 },
11
12 async register(data) {
13 return await axios
14 .post(url+'register/', data)
15 .then(res => res.data)
16 }
17} \ 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 @@
1import Vue from 'vue'
2import Vuex from 'vuex'
3import Axios from 'axios'
4import createPersistedState from "vuex-persistedstate";
5
6Vue.use(Vuex)
7
8const get_default_state = () => {
9 return {
10 token: "",
11 user: {}
12 }
13}
14
15const store = new Vuex.Store({
16 plugins: [createPersistedState()],
17 state: get_default_state(),
18
19 mutations: {
20 set_token: (state, token) => state.token = token,
21 set_user: (state, user) => state.user = user,
22 reset_state: (state) => Object.assign(state, get_default_state())
23 },
24
25 getters: {
26 is_logged_in: (state) => {
27 return state.token !== ""
28 },
29
30 get_user: (state) => {
31 return state.user
32 }
33 },
34
35 actions: {
36 login: ({commit}, {token, user}) => {
37 commit('set_token', token)
38 commit('set_user', user)
39
40 Axios.defaults.headers.common['Authorization'] = `Token ${token}`;
41 },
42 logout: ({commit}) => {
43 commit('reset_state')
44 }
45 }
46})
47
48export 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 @@
1<template>
2 <div class="home">
3 Hi {{get_username}}!
4 <input type="button" value="Logout!" @click="logout">
5 </div>
6</template>
7
8<script>
9export default {
10 methods: {
11 logout() {
12 this.$store.dispatch('logout')
13 this.$router.push('/login')
14 }
15 },
16
17 computed: {
18 get_username() {
19 return this.$store.getters.get_user.username
20 }
21 },
22
23 created() {
24 if(!this.$store.getters['is_logged_in']) {
25 this.$router.push('/login')
26 }
27 }
28
29}
30</script>
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 @@
1<template>
2 <div id="login">
3 <form @submit="log_in">
4 <input type="text" placeholder="Login" v-model="login">
5 <input type="password" placeholder="Password" v-model="password">
6
7 <input type="submit" value="Login!">
8 </form>
9 <p v-for="(mess, key) in messages" :key="key">{{mess}}</p>
10
11 <router-link to="/register"> Still don't have account? </router-link>
12
13 </div>
14</template>
15
16<script>
17import AuthService from '@/services/AuthService'
18
19export default {
20 data: () => {
21 return {
22 login: "",
23 password: "",
24 messages: []
25 }
26 },
27 methods: {
28 async log_in(e) {
29 e.preventDefault()
30
31 const data = {
32 "username": this.login,
33 "password": this.password
34 }
35 try{
36 const res = await AuthService.login(data)
37 this.$store.dispatch('login', {token: res.token, user: res.user})
38 this.$router.push('/')
39 } catch (error) {
40 console.log(error.response.data)
41 this.messages.push('Wrong username or password!')
42 }
43 }
44 },
45 created() {
46 if(this.$store.getters['is_logged_in']) {
47 this.$router.push('/')
48 }
49 }
50
51}
52</script>
53
54<style>
55
56</style> \ 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 @@
1<template>
2 <div id="register">
3 <form @submit="register">
4 <input type="text" placeholder="Login" v-model="login">
5 <input type="password" placeholder="Password" v-model="password">
6 <input type="password" placeholder="Repeat password" v-model="repeat_password">
7
8 <input type="submit" value="Register!">
9 </form>
10 <p v-for="(mess, key) in messages" :key="key">{{mess}}</p>
11
12 <router-link to="/login">Already have account?</router-link>
13
14 </div>
15</template>
16
17<script>
18import AuthService from '@/services/AuthService'
19
20export default {
21 data: () => {
22 return {
23 login: "",
24 password: "",
25 repeat_password: "",
26 messages: []
27 }
28 },
29 methods: {
30 async register(e) {
31 this.messages = []
32
33 e.preventDefault()
34 if(this.password !== this.repeat_password){
35 this.messages.push("Passwords not equals")
36 this.password = ""
37 this.repeat_password = ""
38
39 return null
40 }
41
42 const data = {
43 "username": this.login,
44 "password": this.password
45 }
46
47 try {
48 await AuthService.register(data)
49 this.messages.push("Success!")
50 }catch (error) {
51 if(error.response.data.username)
52 this.messages.push(error.response.data.username[0])
53 if (error.response.data.password)
54 this.messages.push(error.response.data.password[0])
55
56 }
57 }
58 },
59 created() {
60 if(this.$store.getters['is_logged_in']) {
61 this.$router.push('/')
62 }
63 }
64
65}
66</script>
67
68<style>
69
70</style> \ No newline at end of file