mirror of
https://github.com/temetro/temetro.git
synced 2026-08-28 19:37:33 +00:00
Default active organization on session create
Returning members were sent to onboarding on every sign-in because the new session had no activeOrganizationId. Add a session.create `before` hook that defaults it to the user's first clinic membership, so sign-in lands straight in the app. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { betterAuth } from "better-auth";
|
import { betterAuth } from "better-auth";
|
||||||
import { drizzleAdapter } from "better-auth/adapters/drizzle";
|
import { drizzleAdapter } from "better-auth/adapters/drizzle";
|
||||||
import { organization } from "better-auth/plugins";
|
import { organization } from "better-auth/plugins";
|
||||||
|
import { eq } from "drizzle-orm";
|
||||||
|
|
||||||
import { db } from "./db/index.js";
|
import { db } from "./db/index.js";
|
||||||
import * as authSchema from "./db/schema/auth.js";
|
import * as authSchema from "./db/schema/auth.js";
|
||||||
@@ -103,6 +104,20 @@ export const auth = betterAuth({
|
|||||||
databaseHooks: {
|
databaseHooks: {
|
||||||
session: {
|
session: {
|
||||||
create: {
|
create: {
|
||||||
|
before: async (session) => {
|
||||||
|
// Default the active organization to the user's first clinic so
|
||||||
|
// returning members skip onboarding on sign-in.
|
||||||
|
const rows = await db
|
||||||
|
.select({ organizationId: authSchema.member.organizationId })
|
||||||
|
.from(authSchema.member)
|
||||||
|
.where(eq(authSchema.member.userId, session.userId))
|
||||||
|
.limit(1);
|
||||||
|
const organizationId = rows[0]?.organizationId;
|
||||||
|
if (organizationId) {
|
||||||
|
return { data: { ...session, activeOrganizationId: organizationId } };
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
},
|
||||||
after: async (session) => {
|
after: async (session) => {
|
||||||
// Lightweight audit trail; swap console for a real sink in prod.
|
// Lightweight audit trail; swap console for a real sink in prod.
|
||||||
console.info(
|
console.info(
|
||||||
|
|||||||
Reference in New Issue
Block a user