From 4298c4236e0a5c3ef9b6804b0ec51579258765b6 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Thu, 15 Mar 2018 22:34:00 +0100
Subject: [PATCH] Fixed #119: forced redirection to login even with
 API_AUTHENTICATION_REQUIRED=False

---
 api/funkwhale_api/music/views.py         | 1 +
 changes/changelog.d/119.bugfix           | 1 +
 front/src/App.vue                        | 3 +++
 front/src/components/audio/SearchBar.vue | 3 +++
 front/src/components/library/Home.vue    | 2 +-
 front/src/components/library/Library.vue | 7 +++++--
 6 files changed, 14 insertions(+), 3 deletions(-)
 create mode 100644 changes/changelog.d/119.bugfix

diff --git a/api/funkwhale_api/music/views.py b/api/funkwhale_api/music/views.py
index 78a35887..0d33855a 100644
--- a/api/funkwhale_api/music/views.py
+++ b/api/funkwhale_api/music/views.py
@@ -240,6 +240,7 @@ class TagViewSet(viewsets.ReadOnlyModelViewSet):
 
 class Search(views.APIView):
     max_results = 3
+    permission_classes = [ConditionalAuthentication]
 
     def get(self, request, *args, **kwargs):
         query = request.GET['query']
diff --git a/changes/changelog.d/119.bugfix b/changes/changelog.d/119.bugfix
new file mode 100644
index 00000000..b2e7ff71
--- /dev/null
+++ b/changes/changelog.d/119.bugfix
@@ -0,0 +1 @@
+Fixed forced redirection to login event with API_AUTHENTICATION_REQUIRED=False (#119)
diff --git a/front/src/App.vue b/front/src/App.vue
index b26959fe..347f19e3 100644
--- a/front/src/App.vue
+++ b/front/src/App.vue
@@ -56,6 +56,9 @@ export default {
   },
   methods: {
     openWebsocket () {
+      if (!this.$store.state.auth.authenticated) {
+        return
+      }
       let self = this
       let token = this.$store.state.auth.token
       // let token = 'test'
diff --git a/front/src/components/audio/SearchBar.vue b/front/src/components/audio/SearchBar.vue
index 988ff0a7..99896d04 100644
--- a/front/src/components/audio/SearchBar.vue
+++ b/front/src/components/audio/SearchBar.vue
@@ -30,6 +30,9 @@ export default {
       },
       apiSettings: {
         beforeXHR: function (xhrObject) {
+          if (!self.$store.state.auth.authenticated) {
+            return xhrObject
+          }
           xhrObject.setRequestHeader('Authorization', self.$store.getters['auth/header'])
           return xhrObject
         },
diff --git a/front/src/components/library/Home.vue b/front/src/components/library/Home.vue
index e4e22fc0..40f6808f 100644
--- a/front/src/components/library/Home.vue
+++ b/front/src/components/library/Home.vue
@@ -20,7 +20,7 @@
         </div>
         <div class="column">
           <h2 class="ui header">Music requests</h2>
-          <request-form></request-form>
+          <request-form v-if="$store.state.auth.authenticated"></request-form>
         </div>
       </div>
     </div>
diff --git a/front/src/components/library/Library.vue b/front/src/components/library/Library.vue
index 6cd15649..c2092212 100644
--- a/front/src/components/library/Library.vue
+++ b/front/src/components/library/Library.vue
@@ -5,7 +5,7 @@
       <router-link class="ui item" to="/library/artists" exact>Artists</router-link>
       <router-link class="ui item" to="/library/radios" exact>Radios</router-link>
       <div class="ui secondary right menu">
-        <router-link class="ui item" to="/library/requests/" exact>
+        <router-link v-if="$store.state.auth.authenticated" class="ui item" to="/library/requests/" exact>
           Requests
           <div class="ui teal label">{{ requestsCount }}</div>
         </router-link>
@@ -32,8 +32,11 @@ export default {
   },
   methods: {
     fetchRequestsCount () {
+      if (!this.$store.state.authenticated) {
+        return
+      }
       let self = this
-      axios.get('requests/import-requests', {params: {status: 'pending'}}).then(response => {
+      axios.get('requests/import-requests/', {params: {status: 'pending'}}).then(response => {
         self.requestsCount = response.data.count
       })
     }
-- 
GitLab