From b137ce9beff035372ece02485402c3876ddcd5a4 Mon Sep 17 00:00:00 2001
From: Fulgen301 <tokmajigeorge@gmail.com>
Date: Sun, 16 Sep 2018 18:18:30 +0200
Subject: ApiAuth: Add registration functionality

---
 src/components/ApiAuth.vue | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

(limited to 'src/components/ApiAuth.vue')

diff --git a/src/components/ApiAuth.vue b/src/components/ApiAuth.vue
index 30c7e14..5139f74 100644
--- a/src/components/ApiAuth.vue
+++ b/src/components/ApiAuth.vue
@@ -11,6 +11,7 @@
           <q-input @keyup.enter="parryLogin" :error="authError !== undefined" v-model="username" name="user" float-label="Username" />
           <q-input @keyup.enter="parryLogin" :error="authError !== undefined" v-model="password" name="pass" type="password" float-label="Password" />
           <q-btn color="positive" @click="parryLogin" icon="fa-sign-in">Parry login</q-btn>
+          <q-btn color="positive" @click="parryRegister">Register</q-btn>
         </div>
       </div>
     </div>
@@ -59,6 +60,7 @@
           username: this.username,
           password: this.password,
         }
+        console.log(params)
         this.authError = undefined
         this.$http.post('/auth', params).then((response) => {
           that.$store.commit('user/setAuthToken', { authToken: response.data.token })
@@ -78,6 +80,28 @@
           }
         })
       },
+      parryRegister() {
+        let that = this
+        let params = {
+          username: this.username,
+          password: this.password
+        }
+        this.authError = undefined
+        this.$http.post('/auth/register', params).catch(error => {
+          switch (error.response.status)
+          {
+            case 303:
+              this.parryLogin()
+              return
+            case 409:
+              this.authError = 'User already exists'
+              break
+            default:
+              this.authError = 'Failed to register'
+          }
+          console.error({error})
+        })
+      },
       checkTokenLoop (retryCount) {
         let token = LocalStorage.get.item('authToken')
         if (token) {
-- 
cgit v1.2.3-54-g00ecf

