From 541fceb8d9990e4481209379bdb87d9bbb2fea0a Mon Sep 17 00:00:00 2001
From: ingrid <ingrimeg@stud.ntnu.no>
Date: Wed, 26 Apr 2023 14:00:57 +0200
Subject: [PATCH] man kan slette account

---
 src/components/EditAccount.vue | 12 ++++++++++--
 src/util/API.js                | 18 ++++++++++++------
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/src/components/EditAccount.vue b/src/components/EditAccount.vue
index 8dcd8a8..988004c 100644
--- a/src/components/EditAccount.vue
+++ b/src/components/EditAccount.vue
@@ -95,8 +95,16 @@ export default {
                 alert("Du må bekrefte at du vil slette konto ved å huke av boksen")
             }
             else {
-                alert("(Denne knappen gjør ingen ting) men  account deleted")
-                API.deleteAccount(this.user.id, token);
+                const id = this.user.id;
+                API.deleteAccount(
+                    id
+                ).then((savedUser)=>{
+                    useAuthStore().setUser(savedUser);
+                    alert("Bruker oppdatert.")
+                }).catch((error)=> {
+                    alert("Det oppsto en feil ved sletting av bruker")
+                    console.log(error)
+                })
             }
         },
     }
diff --git a/src/util/API.js b/src/util/API.js
index 7489117..dbded43 100644
--- a/src/util/API.js
+++ b/src/util/API.js
@@ -110,15 +110,21 @@ export const API = {
      * @param token
      * @returns {Promise<*>}
      */
-    deleteAccount: async (id, token) => {
-        return axios.delete(`${import.meta.env.VITE_BACKEND_URL}/account/{id}`, {
-            headers: { Authorization: `Bearer ${token}` },
+    deleteAccount: async (id) => {
+        const authStore = useAuthStore();
+        if (!authStore.isLoggedIn) {
+            throw new Error();
+        }
+
+        return axios.delete(`${import.meta.env.VITE_BACKEND_URL}/account/${id}`, {
+            headers: { Authorization: `Bearer ${authStore.token}` },
         })
-            .then((response) => {
-                return response.data;
+            .then(() => {
+                authStore.logout()
+                router.push('/login')
             })
             .catch(() => {
-                throw new Error("Account not found or not accessible");
+                throw new Error("");
             });
 
     },
-- 
GitLab