From d1e7d7bf27e25405b6fcdd485e5aa1a3aeb46265 Mon Sep 17 00:00:00 2001
From: jdlugosz963 <jdlugosz963@gmail.com>
Date: Tue, 9 Nov 2021 22:24:46 +0100
Subject: Add login system

---
 src/views/Login.vue | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)
 create mode 100644 src/views/Login.vue

(limited to 'src/views/Login.vue')

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 @@
+<template>
+  <div id="login">
+      <form @submit="log_in">
+        <input type="text" placeholder="Login" v-model="login">
+        <input type="password" placeholder="Password" v-model="password">
+
+        <input type="submit" value="Login!">
+      </form>
+      <p v-for="(mess, key) in messages" :key="key">{{mess}}</p>
+
+      <router-link to="/register"> Still don't have account? </router-link>
+      
+    </div>
+</template>
+
+<script>
+import AuthService from '@/services/AuthService'
+
+export default {
+    data: () => {
+        return {
+            login: "",
+            password: "",
+            messages: []
+        }
+    },
+    methods: {
+        async log_in(e) {
+            e.preventDefault()
+
+            const data = {
+                "username": this.login,
+                "password": this.password
+            }
+            try{
+                const res = await AuthService.login(data)
+                this.$store.dispatch('login', {token: res.token, user: res.user})
+                this.$router.push('/') 
+            } catch (error) {
+                console.log(error.response.data)
+                this.messages.push('Wrong username or password!')
+            }
+        }
+    },
+    created() {
+        if(this.$store.getters['is_logged_in']) {
+            this.$router.push('/')
+        }
+    }
+
+}
+</script>
+
+<style>
+
+</style>
\ No newline at end of file
-- 
cgit v1.2.3

