B

organization-best-practices

作者 better-auth

使用 Better Auth 的 organization skill,設定多租戶組織,管理成員、角色、權限與團隊,並實作 RBAC。適合需要強大組織與存取控制功能的後端開發者。

Stars0
收藏0
評論0
加入時間2026年3月28日
分類後端开发
安裝指令
npx skills add https://github.com/better-auth/skills --skill organization
總覽

概覽

Better Auth 的 organization skill 讓後端開發者能有效管理多租戶組織、成員、團隊及基於角色的存取控制(RBAC)。此技能適用於需要進階使用者管理的專案,如 SaaS 平台、內部工具,或任何用戶隸屬於具有不同角色與權限組織的系統。

使用 organization skill,您可以:

  • 設定組織並自訂建立與成員數量限制
  • 管理邀請並引導新成員加入
  • 定義並指派自訂角色與權限
  • 在組織內建立團隊
  • 實作 RBAC 以確保存取安全

此技能非常適合需要可擴展、安全且彈性組織管理的後端系統團隊。對於沒有組織架構或單一用戶存取的簡單應用則不太適合。

使用方式

安裝步驟

  1. 將 organization 插件加入您的伺服器設定:

    import { betterAuth } from "better-auth";
    import { organization } from "better-auth/plugins";
    
    export const auth = betterAuth({
      plugins: [
        organization({
          allowUserToCreateOrganization: true,
          organizationLimit: 5, // 每位用戶最多可建立的組織數
          membershipLimit: 100, // 每個組織最多成員數
        }),
      ],
    });
    
  2. 將 organization client 插件加入您的客戶端設定:

    import { createAuthClient } from "better-auth/client";
    import { organizationClient } from "better-auth/client/plugins";
    
    export const authClient = createAuthClient({
      plugins: [organizationClient()],
    });
    
  3. 執行資料庫遷移:

    執行以下指令以建立組織、成員及邀請所需的資料表:

    npx @better-auth/cli migrate
    
  4. 確認設定完成:

    檢查資料庫,確保組織、成員與邀請資料表已成功建立。

建立與管理組織

  • 建立組織:

    建立組織的用戶會自動被指派為 owner 角色。

    const createOrg = async () => {
      const { data, error } = await authClient.organization.create({
        name: "My Company",
        slug: "my-company",
        logo: "https://example.com/logo.png",
        metadata: { plan: "pro" },
      });
    };
    
  • 控制誰能建立組織:

    您可以透過自訂插件選項,依據用戶屬性限制組織建立權限。

自訂角色與權限

定義符合您應用需求的自訂角色與權限。利用插件設定建立 RBAC,並在組織或團隊層級管理存取權限。

最佳實踐

  • 詳細設定選項與進階用法請參閱 SKILL.md 檔案。
  • 根據您的專案結構與需求調整範例程式碼。
  • 適用於需要用戶分組、團隊管理及細緻存取控制的專案。

常見問題

誰適合使用 organization skill?

後端開發者打造多租戶應用、SaaS 產品或任何需要強大組織與存取控制功能的系統,將從此技能中獲益良多。

安裝前需要準備什麼?

您需要一個使用 Better Auth 的專案,並能存取後端與資料庫以進行插件設定與資料庫遷移。

可以自訂組織限制與角色嗎?

可以,organization skill 允許您設定每位用戶可建立的組織數、每個組織的成員數上限,並定義自訂角色與權限。

哪裡可以找到更多細節?

請參考倉庫中的 SKILL.md 檔案以獲得進階設定,或瀏覽 Files 標籤查看相關腳本與參考資料。

評分與評論

尚無評分
分享你的評論
登入後即可為這項技能評分並留言。
G
0/10000
最新評論
儲存中...