Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid token specified React app on Embedding Apache Superset

I want to embed Apache Superset to my React app.

In superset_config.py I added: FEATURE_FLAGS = {..., "EMBEDDED_SUPERSET": True}.

Embed dashboard->Settings->Allowed Domains: specified "http://localhost:3000"

Added "myUser" with roles: Public, Gamma.

In the react app folder I run: npm i @superset-ui/embedded-sdk.

App.js:

import { useEffect } from "react"
import { embedDashboard } from "@superset-ui/embedded-sdk"
import "./App.css"

function App() {
  const getToken = async () => {
return {
    "token": "token_Value"              //get from Insomnia
       }
    }
  useEffect(() => {
    const embed = async () => {
      await embedDashboard({
        id: "dashboardId", // given by the Superset embedding UI
        supersetDomain: "http://localhost:8088",
        mountPoint: document.getElementById("dashboard"), // html element in which iframe render
        fetchGuestToken: () => getToken(),
        dashboardUiConfig: {
          hideTitle: true,
          hideChartControls: true,
          hideTab: true,
        },
      })
    }
    if (document.getElementById("dashboard")) {
      embed()
    }
  }, [])

  return (
    <div className="App">
      <h1>How to Embed Superset Dashboard into React</h1>
      <div id="dashboard" />
    </div>
  )
}

export default App

Value of token for App.js get in Insomnia:

  1. POST http://localhost:8088/api/v1/security/login
  • JSON
{
     "username": "admin",
      "password": "admin",
      "provider": "db",
      "refresh": true
}
  • header
Content-type application/json
  • response
{
    "access_token": "access_token_Value",
    "refresh_token": "refresh_token_Value"
}
  1. GET http://localhost:8088/api/v1/security/csrf_token
  • header
Content-type application/json
Authorization "Bearer ${access_token_Value}"
  • response
{
    "result": "csrf_token_value"
}
  1. POST http://localhost:8088/api/v1/security/guest_token
  • JSON
{
    "resources":
        [
            {
                "type": "dashboard",
                "id": "dashboardId"
            }
        ],
    "rls": [],
    "user": 
        {
            "username": "myUser.name",
            "first_name": "myUser.first",
            "last_name": "myUser.last"
        }
}
  • header
Content-type application/json
X-CSRF-Token "csrf_token_value"
Authorization "Bearer ${access_token_Value}"
  • response
{
    "token": "token_value" //set to App.js
}

When running npm start I get:

ERROR

Invalid token specified
./node_modules/jwt-decode/build/jwt-decode.esm.js
./node_modules/@superset-ui/embedded-sdk/lib/guestTokenRefresh.js

How fix error?

like image 227
IlHam13 Avatar asked Nov 27 '25 14:11

IlHam13


1 Answers

Reason of error: need to return not

const getToken = async () => {
return {
    "token": "token_Value"              //get from Insomnia
       }
    }

need to return

const getToken = async () => {
return "token_Value"              //get from Insomnia
    }
like image 148
IlHam13 Avatar answered Nov 30 '25 03:11

IlHam13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!