Guest User

Untitled

a guest
Aug 7th, 2025
7
0
1 day
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { betterAuth } from "better-auth";
  2. import { nextCookies } from "better-auth/next-js";
  3. import { admin, organization } from "better-auth/plugins";
  4. import { customSession } from "better-auth/plugins";
  5. import { resend } from "./lib/serverUtils";
  6.  
  7. import { adminClient } from "better-auth/client/plugins";
  8. import { db } from "./kysley";
  9. import { getActiveOrganization } from "../app/(norwegian)/kundeportal/kundeportalFunctions";
  10.  
  11. type OrgMetadata = {
  12.   umbracoOrgId?: string;
  13.   umbracoSlug?: string;
  14. };
  15.  
  16. declare module "better-auth" {
  17.  
  18.   interface User {
  19.  
  20.   }
  21.   interface SessionData {
  22.     umbracoId?: string | null;
  23.     umbracoSlug?: string | null;
  24.   }
  25. }
  26.  
  27. export const auth = betterAuth({
  28.   database: {
  29.     db: db,
  30.     type: "postgres",
  31.   },
  32.   emailAndPassword: {
  33.     enabled: true,
  34.     sendResetPassword: async ({ user, url }) => {
  35.       await resend.emails.send({
  36.         from: "test",
  37.         to: user.email,
  38.         subject: "Email Verification",
  39.         html: `test: ${url}`,
  40.       });
  41.     },
  42.   },
  43.   user: {
  44.     deleteUser: {
  45.       enabled: true,
  46.     },
  47.  
  48.   },
  49.   // emailVerification: false;
  50.   databaseHooks: {
  51.     session: {
  52.       create: {
  53.         before: async (session) => {
  54.           const organization = await getActiveOrganization(session.userId);
  55.           if (!organization) {
  56.             // Decide what to do: throw error, return unmodified session, set to null?
  57.             // Returning unmodified session might be safest initially
  58.             return { data: session };
  59.             // Or explicitly set to null if that's desired
  60.             // return { data: { ...session, activeOrganizationId: null } };
  61.           }
  62.           return {
  63.             data: {
  64.               ...session,
  65.               activeOrganizationId: organization.id, // <-- This sets it in the DB session record
  66.             },
  67.           };
  68.         },
  69.       },
  70.     },
  71.   },
  72.  
  73.   plugins: [
  74.     organization(),
  75.     admin(),
  76.     adminClient(),
  77.     customSession(async ({ user, session }) => {
  78.       const activeOrg = await getActiveOrganization(session.userId);
  79.       const metadata = activeOrg?.metadata
  80.         ? (JSON.parse(activeOrg.metadata) as OrgMetadata)
  81.         : null;
  82.       const umbracoId = metadata?.umbracoOrgId || null;
  83.  
  84.       return {
  85.         user,
  86.         session: {
  87.           ...session,
  88.           activeOrganizationId: activeOrg?.id ?? null,
  89.           umbracoId,
  90.         },
  91.       };
  92.     }),
  93.     nextCookies(),
  94.   ],
  95. });
  96.  
  97. export type Session = typeof auth.$Infer.Session;
  98.  
Advertisement
Add Comment
Please, Sign In to add comment