feat: Add tenant table/logic (#50)

This commit is contained in:
Mostapha El Sabah 2025-01-14 22:29:24 -05:00 committed by GitHub
parent a37a57b437
commit fd705aecc5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 92 additions and 3 deletions

View file

@ -14,7 +14,7 @@ export async function validateSession({ locals }: ServerLoadEvent) {
return redirect(307, '/login');
}
if (!locals.auth.orgId && locals.auth.sessionId) {
if ((!locals.auth.orgId && locals.auth.sessionId) || !locals.auth.orgId) {
// Sign out the user if they are not associated with an organization
await clerkSessionClient.sessions.revokeSession(locals.auth.sessionId);
return redirect(307, '/login');
@ -22,9 +22,32 @@ export async function validateSession({ locals }: ServerLoadEvent) {
const clerkUser = await clerkClient.users.getUser(locals.auth.userId);
const tenantClerkId = locals.auth.orgId;
let tenant = await prisma.tenant.findUnique({
where: {
clerkOrganizationId: tenantClerkId,
},
});
if (!tenant) {
const organization = await clerkClient.organizations.getOrganization({
organizationId: tenantClerkId,
});
tenant = await prisma.tenant.create({
data: {
clerkOrganizationId: tenantClerkId,
name: organization.name,
slug: organization.slug ?? `tenant-${tenantClerkId}`,
},
});
}
let user = await prisma.user.findFirst({
where: {
clerkId: clerkUser.id,
tenantId: tenant.id,
},
});
@ -41,6 +64,7 @@ export async function validateSession({ locals }: ServerLoadEvent) {
clerkId: clerkUser.id,
email: clerkUser.emailAddresses[0].emailAddress,
name: clerkUser.fullName ?? '',
tenantId: tenant.id,
},
});

View file

@ -19,6 +19,8 @@ builder.queryFields((t) => ({
users: t.prismaField({
type: [User],
resolve: async () => {
// TODO: Fix this when we add a tenant context
// eslint-disable-next-line no-restricted-syntax
return await prisma.user.findMany();
},
}),