|
| 1 | +--- |
| 2 | +title: Organizations, authentication and authorization |
| 3 | +sidebar: |
| 4 | + label: Organizations and authentication |
| 5 | + order: 10 |
| 6 | +--- |
| 7 | + |
| 8 | +Creating organizations is permission-less. Any user can create an organization and will be the owner. |
| 9 | +Then the owner can add members with admin role and those admins will be able to add more members with different roles. |
| 10 | + |
| 11 | +Better auth client and viem are the recommended libraries to use for authentication and signing using SIWE. |
| 12 | + |
| 13 | +## SIWE Authentication |
| 14 | + |
| 15 | +Example code to authenticate using SIWE, it will create the user if doesn't exist. |
| 16 | +Note: Check viem account to use a private key instead of a mnemonic. |
| 17 | + |
| 18 | +```typescript |
| 19 | +import { createAuthClient } from "better-auth/client"; |
| 20 | +import { siweClient, organizationClient } from "better-auth/client/plugins"; |
| 21 | +import { mnemonicToAccount } from "viem/accounts"; |
| 22 | +import { optimismSepolia } from "viem/chains"; |
| 23 | +import { createSiweMessage } from "viem/siwe"; |
| 24 | + |
| 25 | +const chainId = optimismSepolia.id; |
| 26 | + |
| 27 | +const authClient = createAuthClient({ |
| 28 | + baseURL: "http://localhost:3000", |
| 29 | + plugins: [siweClient(), organizationClient()], |
| 30 | +}); |
| 31 | + |
| 32 | +const owner = mnemonicToAccount("test test test test test test test test test test test test"); |
| 33 | + |
| 34 | +authClient.siwe |
| 35 | + .nonce({ |
| 36 | + walletAddress: owner.address, |
| 37 | + chainId, |
| 38 | + }) |
| 39 | + .then(async ({ data: nonceResult }) => { |
| 40 | + //can be any statement |
| 41 | + const statement = "i accept exa terms and conditions"; |
| 42 | + const nonce = nonceResult?.nonce ?? ""; |
| 43 | + const message = createSiweMessage({ |
| 44 | + statement, |
| 45 | + resources: ["https://exactly.github.io/exa"], |
| 46 | + nonce, |
| 47 | + uri: "https://localhost", |
| 48 | + address: owner.address, |
| 49 | + chainId, |
| 50 | + scheme: "https", |
| 51 | + version: "1", |
| 52 | + domain: "localhost", |
| 53 | + }); |
| 54 | + const signature = await owner.signMessage({ message }); |
| 55 | + |
| 56 | + await authClient.siwe.verify( |
| 57 | + { |
| 58 | + message, |
| 59 | + signature, |
| 60 | + walletAddress: owner.address, |
| 61 | + chainId, |
| 62 | + }, |
| 63 | + { |
| 64 | + onSuccess: async (context) => { |
| 65 | + // authentication successful, session cookie is now set |
| 66 | + }, |
| 67 | + onError: (context) => { |
| 68 | + console.log("authorization error", context); |
| 69 | + }, |
| 70 | + }, |
| 71 | + ); |
| 72 | + }).catch((error: unknown) => { |
| 73 | + console.error("nonce error", error); |
| 74 | + }); |
| 75 | +``` |
| 76 | + |
| 77 | +## Creating an organization |
| 78 | + |
| 79 | +owner account will be the owner of the created organization |
| 80 | + |
| 81 | +```typescript |
| 82 | +const chainId = optimismSepolia.id; |
| 83 | + |
| 84 | +const authClient = createAuthClient({ |
| 85 | + baseURL: "http://localhost:3000", |
| 86 | + plugins: [siweClient(), organizationClient()], |
| 87 | +}); |
| 88 | + |
| 89 | +const owner = mnemonicToAccount("test test test test test test test test test test test siwe"); |
| 90 | + |
| 91 | +authClient.siwe |
| 92 | + .nonce({ |
| 93 | + walletAddress: owner.address, |
| 94 | + chainId, |
| 95 | + }) |
| 96 | + .then(async ({ data: nonceResult }) => { |
| 97 | + const statement = `i accept exa terms and conditions`; |
| 98 | + const nonce = nonceResult?.nonce ?? ""; |
| 99 | + const message = createSiweMessage({ |
| 100 | + statement, |
| 101 | + resources: ["https://exactly.github.io/exa"], |
| 102 | + nonce, |
| 103 | + uri: `https://localhost`, |
| 104 | + address: owner.address, |
| 105 | + chainId, |
| 106 | + scheme: "https", |
| 107 | + version: "1", |
| 108 | + domain: "localhost", |
| 109 | + }); |
| 110 | + const signature = await owner.signMessage({ message }); |
| 111 | + |
| 112 | + await authClient.siwe.verify( |
| 113 | + { |
| 114 | + message, |
| 115 | + signature, |
| 116 | + walletAddress: owner.address, |
| 117 | + chainId, |
| 118 | + }, |
| 119 | + { |
| 120 | + onSuccess: async (context) => { |
| 121 | + const headers = new Headers(); |
| 122 | + headers.set("cookie", context.response.headers.get("set-cookie") ?? ""); |
| 123 | + const createOrganizationResult = await authClient.organization.create({ |
| 124 | + fetchOptions: { headers }, |
| 125 | + name: "Uphold", |
| 126 | + slug: "uphold", |
| 127 | + keepCurrentActiveOrganization: false, |
| 128 | + }); |
| 129 | + if (createOrganizationResult.data) { |
| 130 | + console.log(`organization created id: ${createOrganizationResult.data.id}`); |
| 131 | + } else { |
| 132 | + console.error("Failed to create organization error:", createOrganizationResult.error); |
| 133 | + } |
| 134 | + }, |
| 135 | + onError: (context) => { |
| 136 | + console.log("authorization error", context); |
| 137 | + }, |
| 138 | + }, |
| 139 | + ); |
| 140 | + }).catch((error: unknown) => { |
| 141 | + console.error("nonce error", error); |
| 142 | + }); |
| 143 | + ``` |
0 commit comments