setup-flows-auth
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSet Up Flows Authentication
配置Flows认证
Wire a React app for Flows auth so it can talk to CDF inside Fusion. Two flows exist; pick one based on .
app.json为React应用配置Flows认证,使其能在Fusion内部与CDF通信。存在两种流模式,需根据选择。
app.jsonPick the flow
选择流模式
Read if present:
app.json | Flow | Auth source | Extra package |
|---|---|---|---|
| Apps API (new Fusion app host) | | |
| missing / other | Classic (legacy Files API) | | — |
No ? Ask the user. Default to Apps API — it's the default for .
app.jsonnpx @cognite/dune create若存在,请读取该文件:
app.json | 流模式 | 认证来源 | 额外包 |
|---|---|---|---|
| Apps API(新版Fusion应用宿主) | | |
| 缺失/其他值 | 经典模式(旧版Files API) | | — |
若没有?请询问用户。默认选择Apps API——这是的默认模式。
app.jsonnpx @cognite/dune createStep 1 — Read state, decide whether to act
步骤1 — 读取状态,判断是否需要执行操作
Read , (or ), , .
package.jsonsrc/main.tsxsrc/index.tsxvite.config.tsapp.jsonA valid setup already exists if any of these is true — in which case do nothing and report no-op:
- Classic: from
<DuneAuthProvider>wraps@cognite/dunein the entry file.<App /> - Apps API, generator pattern: from
connectToHostAppis called inside@cognite/app-sdk(or any component) and feeds the auth state into the rest of the app.App.tsx - Apps API, wrapper pattern: from
<AppSdkAuthProvider>wraps@cognite/dunein the entry file. (This is a valid alternative — same<App />API as classic, less boilerplate. Don't try to "fix" it back to the generator default.)useDune()
Detect the package manager from the lock file ( → pnpm, → yarn, otherwise npm).
pnpm-lock.yamlyarn.lock读取、(或)、、。
package.jsonsrc/main.tsxsrc/index.tsxvite.config.tsapp.json若满足以下任一条件,则表示已存在有效配置——此时无需执行任何操作,并告知用户无操作:
- 经典模式:入口文件中(来自
<DuneAuthProvider>)包裹了@cognite/dune。<App /> - Apps API生成器模式:(或任意组件)中调用了
App.tsx的@cognite/app-sdk,并将认证状态传入应用其余部分。connectToHostApp - Apps API包裹器模式:入口文件中(来自
<AppSdkAuthProvider>)包裹了@cognite/dune。(这是一种有效的替代方案——与经典模式使用相同的<App />API,样板代码更少。无需将其“修复”回默认的生成器模式。)useDune()
通过锁文件检测包管理器( → pnpm, → yarn,否则为npm)。
pnpm-lock.yamlyarn.lockStep 2 — Install missing deps
步骤2 — 安装缺失的依赖
Required for both flows:
| Package | Type |
|---|---|
| runtime (provides Vite plugin even in Apps API mode) |
| runtime |
| runtime |
| dev |
Apps API only, also install:
| Package | Type |
|---|---|
| runtime |
Skip anything already in . Use the detected package manager (, , ; / for dev deps).
package.jsonpnpm addnpm installyarn add-D--save-dev两种流模式均需安装:
| 包 | 类型 |
|---|---|
| 运行时(即使在Apps API模式下也提供Vite插件) |
| 运行时 |
| 运行时 |
| 开发依赖 |
仅Apps API模式还需安装:
| 包 | 类型 |
|---|---|
| 运行时 |
跳过已在中的包。使用检测到的包管理器(、、;开发依赖需添加 / 参数)。
package.jsonpnpm addnpm installyarn add-D--save-devStep 3 — Vite config
步骤3 — Vite配置
vite.config.tsts
import { fusionOpenPlugin } from "@cognite/dune/vite";
import mkcert from "vite-plugin-mkcert";
export default defineConfig({
base: "./",
plugins: [react(), mkcert(), fusionOpenPlugin(), /* ... */],
server: { port: 3001 },
worker: { format: "es" },
});- — required for Fusion iframe deployment.
base: "./" - — provides HTTPS for the dev server (the Fusion parent is HTTPS).
mkcert() - — opens the dev URL inside Fusion automatically.
fusionOpenPlugin() - — convention; the plugin falls back to 3001 if no port is set.
server.port: 3001
Add only what's missing. Don't remove existing plugins.
vite.config.tsts
import { fusionOpenPlugin } from "@cognite/dune/vite";
import mkcert from "vite-plugin-mkcert";
export default defineConfig({
base: "./",
plugins: [react(), mkcert(), fusionOpenPlugin(), /* ... */],
server: { port: 3001 },
worker: { format: "es" },
});- — Fusion iframe部署必需。
base: "./" - — 为开发服务器提供HTTPS(Fusion宿主为HTTPS)。
mkcert() - — 自动在Fusion内部打开开发URL。
fusionOpenPlugin() - — 约定;若未设置端口,插件会回退到3001。
server.port: 3001
仅添加缺失的内容,不要移除现有插件。
Step 4 — Wire up the entry file and component
步骤4 — 配置入口文件与组件
Classic flow
经典模式
src/main.tsxtsx
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>
);In components, use :
useDune()tsx
import { useDune } from "@cognite/dune";
const { sdk, isLoading, error } = useDune();
// sdk is an authenticated CogniteClientsrc/main.tsxtsx
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>
);在组件中使用:
useDune()tsx
import { useDune } from "@cognite/dune";
const { sdk, isLoading, error } = useDune();
// sdk为已认证的CogniteClientApps API flow (generator default)
Apps API模式(生成器默认)
src/main.tsxApp.tsxtsx
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}>
<App />
</QueryClientProvider>
</React.StrictMode>
);src/App.tsxconnectToHostApp@cognite/app-sdktsx
import { connectToHostApp } from "@cognite/app-sdk";
import { useEffect, useState } from "react";
function App() {
const [project, setProject] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | undefined>();
useEffect(() => {
let cancelled = false;
connectToHostApp({ applicationName: "<your-app-name>" })
.then(async ({ api }) => {
if (cancelled) return;
setProject(await api.getProject());
})
.catch((err: unknown) => {
if (cancelled) return;
setError(err instanceof Error ? err.message : String(err));
})
.finally(() => {
if (!cancelled) setIsLoading(false);
});
return () => { cancelled = true; };
}, []);
// render isLoading / error / authenticated UI
}Use (from ) so the host can identify the app.
applicationName: appConfig.externalIdapp.jsonsrc/main.tsxApp.tsxtsx
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}>
<App />
</QueryClientProvider>
</React.StrictMode>
);src/App.tsx@cognite/app-sdkconnectToHostApptsx
import { connectToHostApp } from "@cognite/app-sdk";
import { useEffect, useState } from "react";
function App() {
const [project, setProject] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | undefined>();
useEffect(() => {
let cancelled = false;
connectToHostApp({ applicationName: "<your-app-name>" })
.then(async ({ api }) => {
if (cancelled) return;
setProject(await api.getProject());
})
.catch((err: unknown) => {
if (cancelled) return;
setError(err instanceof Error ? err.message : String(err));
})
.finally(() => {
if (!cancelled) setIsLoading(false);
});
return () => { cancelled = true; };
}, []);
// 渲染加载中/错误/已认证UI
}使用(来自),以便宿主识别应用。
applicationName: appConfig.externalIdapp.jsonApps API flow — wrapper alternative
Apps API模式——包裹器替代方案
If the project already uses from , leave it. It wraps the same handshake and gives a API identical to the classic flow. Both patterns are valid for Apps API mode.
<AppSdkAuthProvider>@cognite/duneconnectToHostAppuseDune()若项目已使用的,请保留该配置。它封装了相同的握手流程,并提供与经典模式完全一致的 API。两种模式在Apps API下均有效。
@cognite/dune<AppSdkAuthProvider>connectToHostAppuseDune()Step 5 — Clean up superseded code
步骤5 — 清理过时代码
Remove only what's now redundant:
- Custom CDF auth providers/hooks
- Manual instantiation
CogniteClient - OIDC/token-management code
- CDF env vars (,
VITE_CDF_PROJECT, etc.) — Flows/the host provide theseVITE_CDF_CLUSTER
If unsure, leave it and flag to the user.
仅移除现在冗余的代码:
- 自定义CDF认证提供程序/钩子
- 手动实例化
CogniteClient - OIDC/令牌管理代码
- CDF环境变量(、
VITE_CDF_PROJECT等)——Flows/宿主会提供这些变量VITE_CDF_CLUSTER
若不确定,请保留代码并告知用户。