Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { betterAuth } from "better-auth";
- import { nextCookies } from "better-auth/next-js";
- import { admin, organization } from "better-auth/plugins";
- import { customSession } from "better-auth/plugins";
- import { resend } from "./lib/serverUtils";
- import { adminClient } from "better-auth/client/plugins";
- import { db } from "./kysley";
- import { getActiveOrganization } from "../app/(norwegian)/kundeportal/kundeportalFunctions";
- type OrgMetadata = {
- umbracoOrgId?: string;
- umbracoSlug?: string;
- };
- declare module "better-auth" {
- interface User {
- }
- interface SessionData {
- umbracoId?: string | null;
- umbracoSlug?: string | null;
- }
- }
- export const auth = betterAuth({
- database: {
- db: db,
- type: "postgres",
- },
- emailAndPassword: {
- enabled: true,
- sendResetPassword: async ({ user, url }) => {
- await resend.emails.send({
- from: "test",
- to: user.email,
- subject: "Email Verification",
- html: `test: ${url}`,
- });
- },
- },
- user: {
- deleteUser: {
- enabled: true,
- },
- },
- // emailVerification: false;
- databaseHooks: {
- session: {
- create: {
- before: async (session) => {
- const organization = await getActiveOrganization(session.userId);
- if (!organization) {
- // Decide what to do: throw error, return unmodified session, set to null?
- // Returning unmodified session might be safest initially
- return { data: session };
- // Or explicitly set to null if that's desired
- // return { data: { ...session, activeOrganizationId: null } };
- }
- return {
- data: {
- ...session,
- activeOrganizationId: organization.id, // <-- This sets it in the DB session record
- },
- };
- },
- },
- },
- },
- plugins: [
- organization(),
- admin(),
- adminClient(),
- customSession(async ({ user, session }) => {
- const activeOrg = await getActiveOrganization(session.userId);
- const metadata = activeOrg?.metadata
- ? (JSON.parse(activeOrg.metadata) as OrgMetadata)
- : null;
- const umbracoId = metadata?.umbracoOrgId || null;
- return {
- user,
- session: {
- ...session,
- activeOrganizationId: activeOrg?.id ?? null,
- umbracoId,
- },
- };
- }),
- nextCookies(),
- ],
- });
- export type Session = typeof auth.$Infer.Session;
Advertisement
Add Comment
Please, Sign In to add comment