wip: clerk auth
This commit is contained in:
@@ -1,37 +1,52 @@
|
||||
import { auth, clerkClient } from '@clerk/nextjs';
|
||||
import type { Organization } from '@clerk/nextjs/dist/types/server';
|
||||
|
||||
import { db } from '../db';
|
||||
|
||||
export type IServiceOrganization = Awaited<
|
||||
ReturnType<typeof getOrganizations>
|
||||
>[number];
|
||||
|
||||
export function getOrganizations() {
|
||||
return db.organization.findMany({
|
||||
where: {
|
||||
// users: {
|
||||
// some: {
|
||||
// id: '1',
|
||||
// },
|
||||
// }
|
||||
},
|
||||
});
|
||||
function transformOrganization(org: Organization) {
|
||||
return {
|
||||
id: org.id,
|
||||
name: org.name,
|
||||
slug: org.slug,
|
||||
};
|
||||
}
|
||||
|
||||
export function getOrganizationById(id: string) {
|
||||
return db.organization.findUniqueOrThrow({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
});
|
||||
export async function getOrganizations() {
|
||||
const orgs = await clerkClient.organizations.getOrganizationList();
|
||||
return orgs.map(transformOrganization);
|
||||
}
|
||||
|
||||
export function getOrganizationByProjectId(projectId: string) {
|
||||
return db.organization.findFirst({
|
||||
export async function getCurrentOrganization() {
|
||||
const session = auth();
|
||||
if (!session?.orgSlug) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const organization = await clerkClient.organizations.getOrganization({
|
||||
slug: session.orgSlug,
|
||||
});
|
||||
|
||||
return transformOrganization(organization);
|
||||
}
|
||||
|
||||
export function getOrganizationBySlug(slug: string) {
|
||||
return clerkClient.organizations
|
||||
.getOrganization({ slug })
|
||||
.then(transformOrganization);
|
||||
}
|
||||
|
||||
export async function getOrganizationByProjectId(projectId: string) {
|
||||
const project = await db.project.findUniqueOrThrow({
|
||||
where: {
|
||||
projects: {
|
||||
some: {
|
||||
id: projectId,
|
||||
},
|
||||
},
|
||||
id: projectId,
|
||||
},
|
||||
});
|
||||
|
||||
return clerkClient.organizations.getOrganization({
|
||||
slug: project.organization_slug,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user