diff options
Diffstat (limited to 'src/views/Login.vue')
| -rw-r--r-- | src/views/Login.vue | 56 |
1 files changed, 56 insertions, 0 deletions
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> | ||
| 17 | import AuthService from '@/services/AuthService' | ||
| 18 | |||
| 19 | export 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 | ||
