docs: add activeOrganizationId on org schema

This commit is contained in:
Bereket Engida
2024-11-26 20:40:18 +03:00
parent cdc3dedb08
commit bc010da56d
3 changed files with 59 additions and 1 deletions

View File

@@ -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,
],
});

40
demo/nextjs/lib/plugin.ts Normal file
View File

@@ -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;