跳到主要內容
+

useSession

Toolpad 核心公開了一個 API,用於存取當前的身份驗證會期,無論底層使用何種身份驗證供應商。

用法

當在 Toolpad 核心內部使用身份驗證功能時,會建立一個 SessionContext 以在所有 Toolpad 核心元件之間共享會期資訊。這接受來自 AppProvidersession 屬性的預設值。

<AppProvider session={session}>{props.children}</AppProvider>

可以使用您選擇的任何身份驗證供應商來建立 session。您可以透過調用 hook 在 Toolpad 核心元件內部存取 SessionContext 的當前值

import { useSession } from '@toolpad/core/useSession';
const session = useSession();

如果您的會期有額外的資料,您想要在帳戶彈出視窗中顯示,您可以使用會期物件為使用者資訊顯示建立自訂元件

import { Session } from '@toolpad/core/AppProvider';

interface CustomSession extends Session  {
  org: {
    name: string;
    url: string;
    logo: string;
  };
}

function CustomAccountDetails() {

  const session = useSession<CustomSession>();
  return (
    // Use `session.org`
  )
}

以下範例示範了此行為

按下 Enter 開始編輯