From bc010da56d17e6e371a3daac3873e2270ed61a91 Mon Sep 17 00:00:00 2001 From: Bereket Engida Date: Tue, 26 Nov 2024 20:40:18 +0300 Subject: [PATCH] docs: add activeOrganizationId on org schema --- demo/nextjs/lib/auth.ts | 3 +- demo/nextjs/lib/plugin.ts | 40 ++++++++++++++++++++++ docs/content/docs/plugins/organization.mdx | 17 +++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 demo/nextjs/lib/plugin.ts diff --git a/demo/nextjs/lib/auth.ts b/demo/nextjs/lib/auth.ts index 72c629c1..59625fb3 100644 --- a/demo/nextjs/lib/auth.ts +++ b/demo/nextjs/lib/auth.ts @@ -18,6 +18,7 @@ import { MysqlDialect } from "kysely"; import { createPool } from "mysql2/promise"; import { nextCookies } from "better-auth/next-js"; import { customSession } from "./auth/plugins/custom-session"; +import { addAccountToSession } from "./plugin"; const from = process.env.BETTER_AUTH_EMAIL || "delivered@resend.dev"; const to = process.env.TEST_EMAIL || ""; @@ -155,6 +156,6 @@ export const auth = betterAuth({ oneTap(), oAuthProxy(), nextCookies(), - customSession(), + addAccountToSession, ], }); diff --git a/demo/nextjs/lib/plugin.ts b/demo/nextjs/lib/plugin.ts new file mode 100644 index 00000000..2df9c525 --- /dev/null +++ b/demo/nextjs/lib/plugin.ts @@ -0,0 +1,40 @@ +import { BetterAuthPlugin } from "better-auth"; + +export const addAccountToSession = { + id: "add-account-to-session", + hooks: { + after: [ + { + matcher(context) { + return context.path.startsWith("/callback"); + }, + async handler(ctx) { + const sessionCookie = ctx.responseHeader.get( + ctx.context.authCookies.sessionToken.name, + ); + if (!sessionCookie) { + return; + } + const provider = ctx.path.split("/callback")[1]; + if (!provider) { + return; + } + const sessionId = sessionCookie.split(".")[0]; + await ctx.context.internalAdapter.updateSession(sessionId, { + accountId: provider, + }); + }, + }, + ], + }, + schema: { + session: { + fields: { + accountId: { + type: "string", + required: false, + }, + }, + }, + }, +} satisfies BetterAuthPlugin; diff --git a/docs/content/docs/plugins/organization.mdx b/docs/content/docs/plugins/organization.mdx index 12433117..5518ab2d 100644 --- a/docs/content/docs/plugins/organization.mdx +++ b/docs/content/docs/plugins/organization.mdx @@ -742,6 +742,23 @@ Table Name: `invitation` ]} /> +### Session + +Table Name: `session` + +You need to add one more field to the session table to store the active organization id. + + + ### Customizing the Schema To change the schema table name or fields, you can pass `schema` option to the organization plugin.