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/Register.vue | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)
 create mode 100644 src/views/Register.vue

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

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

