extension-core-infrastructure
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCore Infrastructure
核心基础设施
Core infrastructure extension for Caffeine AI.
为Caffeine AI提供的核心基础设施扩展。
Overview
概述
This component provides the foundational infrastructure for all projects: backend connection configuration, Internet Identity authentication hooks, and actor management utilities.
该组件为所有项目提供基础架构支持:后端连接配置、Internet Identity认证钩子以及角色管理工具。
Requirements
依赖要求
Requries
"@icp-sdk/auth": "^7.1.0"
"@icp-sdk/core": "^5.3.0"需要
"@icp-sdk/auth": "^7.1.0"
"@icp-sdk/core": "^5.3.0"Integration
集成方式
Core infrastructure is automatically included in every project. No manual integration steps are required.
核心基础设施会自动包含在每个项目中,无需手动执行集成步骤。
Frontend
前端部分
The core-infrastructure frontend package () is automatically included in every project.
@caffeineai/core-infrastructure核心基础设施前端包()会自动包含在每个项目中。
@caffeineai/core-infrastructureApp Entry Point
应用入口
Wrap the app with and :
InternetIdentityProviderQueryClientProvidertypescript
import { InternetIdentityProvider } from "@caffeineai/core-infrastructure";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import ReactDOM from "react-dom/client";
import App from "./App";
const queryClient = new QueryClient();
ReactDOM.createRoot(document.getElementById("root")!).render(
<QueryClientProvider client={queryClient}>
<InternetIdentityProvider>
<App />
</InternetIdentityProvider>
</QueryClientProvider>,
);使用和包裹应用:
InternetIdentityProviderQueryClientProvidertypescript
import { InternetIdentityProvider } from "@caffeineai/core-infrastructure";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import ReactDOM from "react-dom/client";
import App from "./App";
const queryClient = new QueryClient();
ReactDOM.createRoot(document.getElementById("root")!).render(
<QueryClientProvider client={queryClient}>
<InternetIdentityProvider>
<App />
</InternetIdentityProvider>
</QueryClientProvider>,
);useInternetIdentity()
— Authentication Hook
useInternetIdentity()useInternetIdentity()
— 认证钩子
useInternetIdentity()Provides identity state, login, and logout for Internet Identity.
提供Internet Identity的身份状态、登录和登出功能。
Return Values
返回值
| Field | Type | Description |
|---|---|---|
| | The user's identity (available after login or session restore) |
| | Opens the II popup. Fire-and-forget — do not |
| | Logs out and clears stored identity. Fire-and-forget. |
| | |
| | |
| | |
| | |
| | |
| | The error object when |
| 字段 | 类型 | 描述 |
|---|---|---|
| | 用户身份(登录或会话恢复后可用) |
| | 打开II弹窗。无需等待,直接调用即可。 |
| | 登出并清除存储的身份信息。无需等待,直接调用即可。 |
| | 用户拥有有效身份时为 |
| | |
| | II弹窗打开时为 |
| | 仅在交互式登录完成后为 |
| | 登录或初始化失败时为 |
| | |
Auth State Lifecycle
认证状态生命周期
| Scenario | | |
|---|---|---|
| Page load, no stored session | | |
| Restoring stored session | | |
| Stored session restored after reload | | |
| Interactive login in progress | | |
| Interactive login just completed | | |
| Login popup failed / cancelled | | |
IMPORTANT: is only after an interactive login via the popup — NOT when a stored identity is restored on page reload. Always use for conditional rendering.
isLoginSuccesstrueisAuthenticated| 场景 | | |
|---|---|---|
| 页面加载,无存储会话 | | |
| 恢复存储会话中 | | |
| 页面重载后恢复存储会话 | | |
| 交互式登录进行中 | | |
| 交互式登录刚完成 | | |
| 登录弹窗失败/取消 | | |
重要提示: 仅在通过弹窗完成交互式登录后为——页面重载恢复存储身份时不会为。条件渲染请始终使用。
isLoginSuccesstruetrueisAuthenticatedUsage
使用示例
Gate authenticated UI on :
isAuthenticatedtypescript
const { isAuthenticated } = useInternetIdentity();
{isAuthenticated ? <AuthenticatedApp /> : <LoginScreen />}Disable the login button while initializing or logging in:
typescript
const { login, isInitializing, isLoggingIn } = useInternetIdentity();
<button onClick={() => login()} disabled={isInitializing || isLoggingIn}>
Sign in
</button>login()clear()isLoggingInisInitializinguseStateisPending基于控制认证后UI的显示:
isAuthenticatedtypescript
const { isAuthenticated } = useInternetIdentity();
{isAuthenticated ? <AuthenticatedApp /> : <LoginScreen />}在初始化或登录过程中禁用登录按钮:
typescript
const { login, isInitializing, isLoggingIn } = useInternetIdentity();
<button onClick={() => login()} disabled={isInitializing || isLoggingIn}>
登录
</button>login()clear()isLoggingInisInitializinguseStateisPendinguseActor()
— Backend Actor Hook
useActor()useActor()
— 后端角色钩子
useActor()Creates and manages a typed backend actor instance. Automatically re-creates the actor when the user's identity changes (login/logout).
typescript
import { useActor } from "@caffeineai/core-infrastructure";
import { createActor } from "declarations/backend";
function MyComponent() {
const { actor, isFetching } = useActor(createActor);
// actor is null while loading, then the typed backend actor
if (!actor || isFetching) return <Loading />;
// Call backend methods directly
const data = await actor.myBackendMethod();
}创建并管理类型化的后端角色实例。当用户身份变更(登录/登出)时,会自动重新创建角色实例。
typescript
import { useActor } from "@caffeineai/core-infrastructure";
import { createActor } from "declarations/backend";
function MyComponent() {
const { actor, isFetching } = useActor(createActor);
// 加载时actor为null,之后变为类型化的后端角色
if (!actor || isFetching) return <Loading />;
// 直接调用后端方法
const data = await actor.myBackendMethod();
}Return Values
返回值
| Field | Type | Description |
|---|---|---|
| | The typed backend actor, or |
| | |
When the identity changes (login, logout, or session restore), the actor is automatically re-created with the new identity and all dependent queries are invalidated and refetched.
| 字段 | 类型 | 描述 |
|---|---|---|
| | 类型化的后端角色,加载时为 |
| | 创建角色实例时为 |
当身份变更(登录、登出或会话恢复)时,会使用新身份自动重新创建角色实例,所有相关查询都会失效并重新获取数据。