mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-10 04:19:32 +00:00
docs: add activeOrganizationId on org schema
This commit is contained in:
@@ -18,6 +18,7 @@ import { MysqlDialect } from "kysely";
|
|||||||
import { createPool } from "mysql2/promise";
|
import { createPool } from "mysql2/promise";
|
||||||
import { nextCookies } from "better-auth/next-js";
|
import { nextCookies } from "better-auth/next-js";
|
||||||
import { customSession } from "./auth/plugins/custom-session";
|
import { customSession } from "./auth/plugins/custom-session";
|
||||||
|
import { addAccountToSession } from "./plugin";
|
||||||
|
|
||||||
const from = process.env.BETTER_AUTH_EMAIL || "delivered@resend.dev";
|
const from = process.env.BETTER_AUTH_EMAIL || "delivered@resend.dev";
|
||||||
const to = process.env.TEST_EMAIL || "";
|
const to = process.env.TEST_EMAIL || "";
|
||||||
@@ -155,6 +156,6 @@ export const auth = betterAuth({
|
|||||||
oneTap(),
|
oneTap(),
|
||||||
oAuthProxy(),
|
oAuthProxy(),
|
||||||
nextCookies(),
|
nextCookies(),
|
||||||
customSession(),
|
addAccountToSession,
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|||||||
40
demo/nextjs/lib/plugin.ts
Normal file
40
demo/nextjs/lib/plugin.ts
Normal 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;
|
||||||
@@ -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.
|
||||||
|
|
||||||
|
<DatabaseTable
|
||||||
|
fields={[
|
||||||
|
{
|
||||||
|
name: "activeOrganizationId",
|
||||||
|
type: "string",
|
||||||
|
description: "The id of the active organization",
|
||||||
|
isOptional: true
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
/>
|
||||||
|
|
||||||
### Customizing the Schema
|
### Customizing the Schema
|
||||||
|
|
||||||
To change the schema table name or fields, you can pass `schema` option to the organization plugin.
|
To change the schema table name or fields, you can pass `schema` option to the organization plugin.
|
||||||
|
|||||||
Reference in New Issue
Block a user