fix: reenable member invites

This commit is contained in:
Henry
2025-05-22 12:18:56 +01:00
parent 82ec18e95e
commit 71c92eea27
16 changed files with 2183 additions and 231 deletions

View File

@@ -1,11 +1,10 @@
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { authClient } from "@kan/auth";
import * as memberRepo from "@kan/db/repository/member.repo";
import * as userRepo from "@kan/db/repository/user.repo";
import * as workspaceRepo from "@kan/db/repository/workspace.repo";
import { sendEmail } from "@kan/email";
import { createStripeClient } from "@kan/stripe";
import { createTRPCRouter, protectedProcedure } from "../trpc";
@@ -49,7 +48,7 @@ export const memberRouter = createTRPCRouter({
});
const isInvitedEmailAlreadyMember = workspace.members.some(
(member) => member.user.email === input.email,
(member) => member.email === input.email,
);
if (isInvitedEmailAlreadyMember) {
@@ -59,79 +58,12 @@ export const memberRouter = createTRPCRouter({
});
}
let invitedUserId: string | undefined;
let hashedToken: string | undefined;
let verificationType: string | undefined;
const existingUser = await userRepo.getByEmail(ctx.db, input.email);
// if (existingUser) {
// invitedUserId = existingUser.id;
// const magicLink = await ctx.supabaseClient.auth.admin.generateLink({
// type: "magiclink",
// email: input.email,
// options: {
// redirectTo: process.env.WEBSITE_URL,
// },
// });
// hashedToken = magicLink.data.properties?.hashed_token;
// verificationType = magicLink.data.properties?.verification_type;
// } else {
// const invite = await ctx.supabaseClient.auth.admin.generateLink({
// type: "invite",
// email: input.email,
// options: {
// redirectTo: process.env.WEBSITE_URL,
// },
// });
// hashedToken = invite.data.properties?.hashed_token;
// verificationType = invite.data.properties?.verification_type;
// const invitedUserAuthId = invite.data.user?.id;
// const invitedUserEmail = invite.data.user?.email;
// if (invitedUserAuthId && invitedUserEmail) {
// const stripeCustomer = await stripe.customers.create({
// email: invitedUserEmail,
// metadata: {
// userId: invitedUserAuthId,
// },
// });
// const newUser = await userRepo.create(ctx.db, {
// email: invitedUserEmail,
// id: invitedUserAuthId,
// stripeCustomerId: stripeCustomer.id,
// });
// if (!newUser)
// throw new TRPCError({
// message: `Failed to create a new user for email ${invitedUserEmail}`,
// code: "INTERNAL_SERVER_ERROR",
// });
// invitedUserId = newUser.id;
// }
// }
if (!invitedUserId)
throw new TRPCError({
message: `Unable to invite user with email ${input.email}`,
code: "INTERNAL_SERVER_ERROR",
});
if (!hashedToken || !verificationType)
throw new TRPCError({
message: `Unable to generate magic link for user with email ${input.email}`,
code: "INTERNAL_SERVER_ERROR",
});
const invite = await memberRepo.create(ctx.db, {
workspaceId: workspace.id,
userId: invitedUserId,
email: input.email,
userId: existingUser?.id ?? null,
createdBy: userId,
role: "member",
status: "invited",
@@ -143,16 +75,16 @@ export const memberRouter = createTRPCRouter({
code: "INTERNAL_SERVER_ERROR",
});
const magicLoginUrl = `${process.env.WEBSITE_URL}/api/auth/confirm?token_hash=${hashedToken}&type=${verificationType}&memberPublicId=${invite.publicId}`;
const { error } = await authClient.signIn.magicLink({
email: input.email,
callbackURL: `/boards?type=invite&memberPublicId=${invite.publicId}`,
});
await sendEmail(
input.email,
"Invitation to join workspace",
"JOIN_WORKSPACE",
{
magicLoginUrl,
},
);
if (error)
throw new TRPCError({
message: `Failed to send magic link to user with email ${input.email}`,
code: "INTERNAL_SERVER_ERROR",
});
return invite;
}),

View File

@@ -39,7 +39,7 @@ export const userRouter = createTRPCRouter({
const result = await userRepo.getById(ctx.db, userId);
if (!result?.name) {
if (!result) {
throw new TRPCError({
message: `User not found`,
code: "NOT_FOUND",

View File

@@ -117,8 +117,9 @@ export const workspaceRouter = createTRPCRouter({
.output(z.custom<Awaited<ReturnType<typeof workspaceRepo.create>>>())
.mutation(async ({ ctx, input }) => {
const userId = ctx.user?.id;
const userEmail = ctx.user?.email;
if (!userId)
if (!userId || !userEmail)
throw new TRPCError({
message: `User not authenticated`,
code: "UNAUTHORIZED",
@@ -131,6 +132,7 @@ export const workspaceRouter = createTRPCRouter({
name: input.name,
slug: workspacePublicId,
createdBy: userId,
createdByEmail: userEmail,
});
if (!result.publicId)