setup-dune-auth
Original:🇺🇸 English
Translated
MUST be used when migrating an existing React app to Dune, or when DuneAuthProvider is missing from the app. This skill installs the @cognite/dune package, wraps the app in DuneAuthProvider, configures the Vite plugin, and sets up the useDune hook. Triggers: migrate to Dune, add Dune auth, DuneAuthProvider, useDune, Dune setup, setup auth, missing auth provider, CDF authentication, Fusion iframe auth.
24installs
Sourcecognitedata/dune-skills
Added on
NPX Install
npx skill4agent add cognitedata/dune-skills setup-dune-authTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Set Up Dune Authentication
Migrate an existing React app to Dune by installing auth dependencies, wrapping the app in , and configuring the Vite plugin.
DuneAuthProviderYour job
Complete these steps in order. Read each file before modifying it.
Step 1 — Understand the app
Read these files to understand the current setup:
- — detect package manager (
package.jsonfield or lock file) and existing depspackageManager - (or
src/main.tsx,src/index.tsx) — find the app entry point and root rendersrc/main.ts - (or
vite.config.ts) — check for existing Vite configurationvite.config.js - (or equivalent) — understand the app structure
src/App.tsx
Check what's already in place:
- Is already in
@cognite/dune?package.json - Is already wrapping the app?
DuneAuthProvider - Is already in the Vite config?
fusionOpenPlugin - Is already installed?
@tanstack/react-query
Only fix what's missing — do not duplicate existing setup.
Step 2 — Install dependencies
Install the required packages using the app's package manager. Only install packages that are not already in .
package.jsonRequired dependencies:
| Package | Purpose |
|---|---|
| Auth provider, useDune hook, Vite plugin |
| CogniteClient for CDF API access |
| Required peer dependency for Dune |
Required dev dependencies:
| Package | Purpose |
|---|---|
| HTTPS in local dev (required for Fusion iframe auth) |
Install commands by package manager:
- pnpm →
pnpm add @cognite/dune @cognite/sdk @tanstack/react-query && pnpm add -D vite-plugin-mkcert - npm →
npm install @cognite/dune @cognite/sdk @tanstack/react-query && npm install -D vite-plugin-mkcert - yarn →
yarn add @cognite/dune @cognite/sdk @tanstack/react-query && yarn add -D vite-plugin-mkcert
Skip any package that's already installed.
Step 3 — Configure Vite
Edit to add the and plugins if they are not already present.
vite.config.tsfusionOpenPluginmkcertRequired imports
ts
import { fusionOpenPlugin } from "@cognite/dune/vite";
import mkcert from "vite-plugin-mkcert";Required plugin entries
Add and to the array:
mkcert()fusionOpenPlugin()pluginsts
export default defineConfig({
base: "./",
plugins: [react(), mkcert(), fusionOpenPlugin(), /* ...other plugins */],
server: {
port: 3001,
},
worker: {
format: "es",
},
});Key details:
- is required for Fusion iframe deployment
base: "./" - enables HTTPS locally, which is required for Fusion iframe communication
mkcert() - opens the app inside Fusion during
fusionOpenPlugin()for auth to workpnpm dev - is the conventional Dune dev port (optional but recommended)
server.port: 3001 - is needed if web workers are used (optional but recommended)
worker.format: "es"
Only add what's missing. Do not remove existing plugins.
Step 4 — Wrap the app in DuneAuthProvider
Edit the entry file ( or equivalent) to wrap the app with and .
src/main.tsxDuneAuthProviderQueryClientProviderTarget structure
tsx
import { DuneAuthProvider } from "@cognite/dune";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App.tsx";
const queryClient = new QueryClient({
defaultOptions: {
queries: {
staleTime: 5 * 60 * 1000,
gcTime: 10 * 60 * 1000,
},
},
});
ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<QueryClientProvider client={queryClient}>
<DuneAuthProvider>
<App />
</DuneAuthProvider>
</QueryClientProvider>
</React.StrictMode>
);Important rules:
- must be inside
DuneAuthProviderQueryClientProvider - must be outside (wrapping) the
DuneAuthProvidercomponent<App /> - Keep any existing providers the app already has — nest around the app content but inside
DuneAuthProviderQueryClientProvider - If already exists, just add
QueryClientProviderinside itDuneAuthProvider - If already wraps the app, do nothing
DuneAuthProvider
Step 5 — Wire up useDune in components
Search the app for any existing CogniteClient instantiation or auth setup (e.g. manual , custom auth contexts, or OIDC flows) and replace them with the hook.
new CogniteClient(...)useDuneThe hook API
tsx
import { useDune } from "@cognite/dune";
function MyComponent() {
const { sdk, isLoading, error } = useDune();
if (isLoading) return <div>Loading...</div>;
if (error) return <div>Error: {error}</div>;
// sdk is an authenticated CogniteClient — use it directly
// e.g. sdk.timeseries.list(), sdk.assets.list(), etc.
}Return values:
| Field | Type | Description |
|---|---|---|
| | Authenticated Cognite SDK instance |
| | |
| | Error message if auth fails |
If the app has no existing CDF auth code to replace, skip this step.
Step 6 — Clean up old auth code
If the app previously used a custom auth setup, remove it:
- Delete custom auth context providers or hooks that are now replaced by /
DuneAuthProvideruseDune - Remove manual instantiation from entry files
CogniteClient - Remove OIDC/token management code that now handles
DuneAuthProvider - Remove any environment variables for CDF credentials (e.g. ,
VITE_CDF_PROJECT) — Dune handles this via Fusion iframe messagingVITE_CDF_CLUSTER
Only remove code that is clearly superseded. If unsure, leave it and flag it to the user.
Done
The app should now:
- Have ,
@cognite/dune, and@cognite/sdkinstalled@tanstack/react-query - Have wrapping the app in the entry file
DuneAuthProvider - Have and
fusionOpenPlugin()in the Vite configmkcert() - Use to access the authenticated
useDune()CogniteClient
Run (or the app's dev command) — it should open in Fusion and authenticate automatically.
pnpm dev