setup-dune-auth
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSet Up Dune Authentication
配置Dune认证
Migrate an existing React app to Dune by installing auth dependencies, wrapping the app in , and configuring the Vite plugin.
DuneAuthProvider通过安装认证依赖、用包裹应用并配置Vite插件,将现有React应用迁移至Dune。
DuneAuthProviderYour job
你的任务
Complete these steps in order. Read each file before modifying it.
按顺序完成以下步骤。修改文件前请先阅读每个文件。
Step 1 — Understand the app
步骤1 — 了解应用
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.
阅读以下文件以了解当前配置:
- — 检测包管理器(
package.json字段或锁文件)及现有依赖packageManager - (或
src/main.tsx、src/index.tsx) — 找到应用入口点和根渲染逻辑src/main.ts - (或
vite.config.ts) — 检查现有Vite配置vite.config.js - (或等效文件) — 了解应用结构
src/App.tsx
检查已有的配置:
- 中是否已存在
package.json?@cognite/dune - 应用是否已被包裹?
DuneAuthProvider - Vite配置中是否已存在?
fusionOpenPlugin - 是否已安装?
@tanstack/react-query
仅修复缺失的部分 — 不要重复已有的配置。
Step 2 — Install dependencies
步骤2 — 安装依赖
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.
使用应用的包管理器安装所需包。仅安装未在中存在的包。
package.json必需依赖:
| 包名 | 用途 |
|---|---|
| 认证提供者、useDune钩子、Vite插件 |
| 用于CDF API访问的CogniteClient |
| Dune所需的对等依赖 |
必需开发依赖:
| 包名 | 用途 |
|---|---|
| 本地开发中的HTTPS支持(Fusion iframe认证必需) |
按包管理器分类的安装命令:
- 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
跳过任何已安装的包。
Step 3 — Configure Vite
步骤3 — 配置Vite
Edit to add the and plugins if they are not already present.
vite.config.tsfusionOpenPluginmkcert编辑,添加和插件(如果尚未存在)。
vite.config.tsfusionOpenPluginmkcertRequired imports
必需导入
ts
import { fusionOpenPlugin } from "@cognite/dune/vite";
import mkcert from "vite-plugin-mkcert";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.
将和添加到数组:
mkcert()fusionOpenPlugin()pluginsts
export default defineConfig({
base: "./",
plugins: [react(), mkcert(), fusionOpenPlugin(), /* ...其他插件 */],
server: {
port: 3001,
},
worker: {
format: "es",
},
});关键细节:
- 是Fusion iframe部署的必需配置
base: "./" - 启用本地HTTPS支持,这是Fusion iframe通信的必需条件
mkcert() - 会在
fusionOpenPlugin()时将应用在Fusion中打开,确保认证正常工作pnpm dev - 是Dune开发的常规端口(可选但推荐)
server.port: 3001 - 在使用Web Worker时是必需的(可选但推荐)
worker.format: "es"
仅添加缺失的内容,不要移除现有插件。
Step 4 — Wrap the app in DuneAuthProvider
步骤4 — 用DuneAuthProvider包裹应用
Edit the entry file ( or equivalent) to wrap the app with and .
src/main.tsxDuneAuthProviderQueryClientProvider编辑入口文件(或等效文件),用和包裹应用。
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
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>
);重要规则:
- 必须在
DuneAuthProvider内部QueryClientProvider - 必须在
DuneAuthProvider组件外部(包裹组件)<App /> - 保留应用已有的所有提供者 — 将嵌套在
DuneAuthProvider内部,包裹应用内容QueryClientProvider - 如果已存在,只需在其内部添加
QueryClientProviderDuneAuthProvider - 如果应用已被包裹,则无需操作
DuneAuthProvider
Step 5 — Wire up useDune in components
步骤5 — 在组件中接入useDune
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(...)useDune搜索应用中所有现有的CogniteClient实例化或认证配置(例如手动、自定义认证上下文或OIDC流程),并将其替换为钩子。
new CogniteClient(...)useDuneThe hook API
钩子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.
tsx
import { useDune } from "@cognite/dune";
function MyComponent() {
const { sdk, isLoading, error } = useDune();
if (isLoading) return <div>加载中...</div>;
if (error) return <div>错误: {error}</div>;
// sdk是已认证的CogniteClient — 可直接使用
// 例如 sdk.timeseries.list(), sdk.assets.list(), 等等
}返回值说明:
| 字段 | 类型 | 描述 |
|---|---|---|
| | 已认证的Cognite SDK实例 |
| | 初始认证握手期间为 |
| | 认证失败时的错误信息 |
如果应用没有需要替换的现有CDF认证代码,则跳过此步骤。
Step 6 — Clean up old auth code
步骤6 — 清理旧认证代码
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.
如果应用之前使用自定义认证配置,请移除相关代码:
- 删除已被/
DuneAuthProvider替代的自定义认证上下文提供者或钩子useDune - 移除入口文件中手动的实例化代码
CogniteClient - 移除现在由处理的OIDC/令牌管理代码
DuneAuthProvider - 移除CDF凭证相关的环境变量(例如、
VITE_CDF_PROJECT)— Dune通过Fusion iframe消息传递处理这些内容VITE_CDF_CLUSTER
仅移除明显被替代的代码。若不确定,请保留代码并向用户标注。
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应用现在应具备以下特性:
- 已安装、
@cognite/dune和@cognite/sdk@tanstack/react-query - 入口文件中已用包裹应用
DuneAuthProvider - Vite配置中已添加和
fusionOpenPlugin()mkcert() - 使用访问已认证的
useDune()CogniteClient
运行(或应用的开发命令)— 应用应在Fusion中打开并自动完成认证。
pnpm dev