Browse Source

[api] Automatically grab the JWT token in axios

Romain Rigaux 4 years ago
parent
commit
a32bbd475c

+ 2 - 2
desktop/core/src/desktop/js/api/utils.ts

@@ -70,8 +70,8 @@ axiosInstance.interceptors.request.use(config => {
 });
 
 axiosInstance.interceptors.response.use(response => {
-  if (response.data?.token) {
-    bearerToken = response.data.token;
+  if (response.data?.access) {
+    bearerToken = response.data.access;
   }
   return response;
 });

+ 1 - 1
desktop/core/src/desktop/settings.py

@@ -385,7 +385,7 @@ if desktop.conf.CORS_ENABLED.get():
   MIDDLEWARE.remove('django.middleware.csrf.CsrfViewMiddleware')
   if sys.version_info[0] > 2:
     CORS_ALLOW_ALL_ORIGINS = True
-    # CORS_URLS_REGEX = r'^/api/.*$'
+    # CORS_URLS_REGEX = r'^/api/.*$'  # To activate for v1
   else:
     CORS_ORIGIN_ALLOW_ALL = True
   CORS_ALLOW_CREDENTIALS = True  # For when cookie auth

+ 5 - 5
docs/docs-site/content/developer/api/_index.md

@@ -80,19 +80,19 @@ In the meantime, with Axios:
       const API_URL = "/";
       axios.defaults.baseURL = API_URL;
 
-      axios.post('iam/v1/get/auth-token/', {username: "hue", password: "hue"}).then(function(data) {
+      axios.post('api/iam/token/auth/', {username: "hue", password: "hue"}).then(function(data) {
         console.log(data['data']);
 
         // Util to check if cached token is still valid before asking to auth for a new one
-        axios.post('iam/v1/verify/auth-token/', {token: data['data']['token']});
+        axios.post('api/iam/token/verify/', {token: data['access']});
 
-        axios.defaults.headers.common['Authorization'] = 'Bearer ' + data['data']['token'];
+        axios.defaults.headers.common['Authorization'] = 'Bearer ' + data['access'];
       }).then(function() {
-        axios.post('editor/v1/query/sqlite', {statement:"SELECT 1000, 1001"}).then(function(data) {
+        axios.post('api/query/sqlite', {statement:"SELECT 1000, 1001"}).then(function(data) {
           console.log(data['data']);
         });
 
-        axios.post('connectors/v1/types/').then(function(data) {
+        axios.post('api/connectors/types/').then(function(data) {
           console.log(data['data']);
         });
       });

+ 5 - 0
docs/docs-site/content/developer/components/scratchpad/_index.md

@@ -34,4 +34,9 @@ Note: even simpler you could just skip npm and grab the latest via:
 
 Above expects a running Hue or [Compose](https://github.com/gethue/compose) APIs on http://locahost:8005 as the middleware between your Data Warehouse and client.
 
+Note: if using Hue, it currently temporarily requires this ini settings:
+
+    [desktop]
+    cors_enabled=true
+
 !["SQL Scratchpad"](https://cdn.gethue.com/uploads/2021/05/sql-scratchpad-v0.5.png)