feature(auth): replace clerk.com with custom auth (#103)

* feature(auth): replace clerk.com with custom auth

* minor fixes

* remove notification preferences

* decrease live events interval

fix(api): cookies..

# Conflicts:
#	.gitignore
#	apps/api/src/index.ts
#	apps/dashboard/src/app/providers.tsx
#	packages/trpc/src/trpc.ts
This commit is contained in:
Carl-Gerhard Lindesvärd
2024-12-18 21:30:39 +01:00
committed by Carl-Gerhard Lindesvärd
parent f28802b1c2
commit d31d9924a5
151 changed files with 18484 additions and 12853 deletions

View File

@@ -1,12 +1,14 @@
import { auth } from '@clerk/nextjs/server';
import type { Organization, Prisma, ProjectAccess } from '../prisma-client';
import type {
Invite,
Organization,
Prisma,
ProjectAccess,
User,
} from '../prisma-client';
import { db } from '../prisma-client';
export type IServiceOrganization = ReturnType<typeof transformOrganization>;
export type IServiceInvite = Prisma.MemberGetPayload<{
include: { user: true };
}>;
export type IServiceInvite = Invite;
export type IServiceMember = Prisma.MemberGetPayload<{
include: { user: true };
}> & { access: ProjectAccess[] };
@@ -21,15 +23,14 @@ export function transformOrganization(org: Organization) {
};
}
export async function getCurrentOrganizations() {
const session = auth();
if (!session.userId) return [];
export async function getOrganizations(userId: string | null) {
if (!userId) return [];
const organizations = await db.organization.findMany({
where: {
members: {
some: {
userId: session.userId,
userId,
},
},
},
@@ -67,13 +68,25 @@ export async function getOrganizationByProjectId(projectId: string) {
}
export async function getInvites(organizationId: string) {
return db.member.findMany({
return db.invite.findMany({
where: {
organizationId,
userId: null,
},
});
}
export function getInviteById(inviteId: string) {
return db.invite.findUnique({
where: {
id: inviteId,
},
include: {
user: true,
organization: {
select: {
id: true,
name: true,
},
},
},
});
}
@@ -112,3 +125,56 @@ export async function getMember(organizationId: string, userId: string) {
},
});
}
export async function connectUserToOrganization({
user,
inviteId,
}: {
user: User;
inviteId: string;
}) {
const invite = await db.invite.findUnique({
where: {
id: inviteId,
},
});
if (!invite) {
throw new Error('Invite not found');
}
if (invite.expiresAt < new Date()) {
throw new Error('Invite expired');
}
const member = await db.member.create({
data: {
organizationId: invite.organizationId,
userId: user.id,
role: invite.role,
email: user.email,
invitedById: invite.createdById,
},
});
if (invite.projectAccess.length > 0) {
for (const projectId of invite.projectAccess) {
await db.projectAccess.create({
data: {
projectId,
userId: user.id,
organizationId: invite.organizationId,
level: 'write',
},
});
}
}
await db.invite.delete({
where: {
id: inviteId,
},
});
return member;
}

View File

@@ -1,5 +1,3 @@
import { auth } from '@clerk/nextjs/server';
import { cacheable } from '@openpanel/redis';
import type { Prisma, Project } from '../prisma-client';
import { db } from '../prisma-client';
@@ -55,9 +53,14 @@ export async function getProjectsByOrganizationId(organizationId: string) {
});
}
export async function getCurrentProjects(organizationId: string) {
const session = auth();
if (!session.userId) {
export async function getProjects({
organizationId,
userId,
}: {
organizationId: string;
userId: string | null;
}) {
if (!userId) {
return [];
}
@@ -72,13 +75,13 @@ export async function getCurrentProjects(organizationId: string) {
}),
db.member.findMany({
where: {
userId: session.userId,
userId,
organizationId,
},
}),
db.projectAccess.findMany({
where: {
userId: session.userId,
userId,
organizationId,
},
}),

View File

@@ -1,15 +1,5 @@
import { auth } from '@clerk/nextjs/server';
import { db } from '../prisma-client';
export async function getCurrentUser() {
const session = auth();
if (!session.userId) {
return null;
}
return getUserById(session.userId);
}
export async function getUserById(id: string) {
return db.user.findUniqueOrThrow({
where: {
@@ -17,3 +7,33 @@ export async function getUserById(id: string) {
},
});
}
export async function getUserAccount({
email,
provider,
providerId,
}: { email: string; provider: string; providerId?: string }) {
const res = await db.user.findFirst({
where: {
email,
},
include: {
accounts: {
where: {
provider,
providerId: providerId ? String(providerId) : undefined,
},
take: 1,
},
},
});
if (!res?.accounts[0]) {
return null;
}
return {
...res,
account: res?.accounts[0],
};
}