refactor: remove supabase auth

This commit is contained in:
Henry
2025-05-05 15:16:30 +01:00
parent 091fea6f38
commit b6daaa41de
42 changed files with 319 additions and 1591 deletions

View File

@@ -1,24 +1,14 @@
import { TRPCError } from "@trpc/server";
import { Stripe } from "stripe";
import { z } from "zod";
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";
const stripeSecretKey = process.env.STRIPE_SECRET_KEY;
if (!stripeSecretKey) {
throw new Error("STRIPE_SECRET_KEY is not defined");
}
const stripe = new Stripe(stripeSecretKey, {
apiVersion: "2024-12-18.acacia",
});
export const memberRouter = createTRPCRouter({
invite: protectedProcedure
.meta({
@@ -75,57 +65,57 @@ export const memberRouter = createTRPCRouter({
const existingUser = await userRepo.getByEmail(ctx.db, input.email);
if (existingUser) {
invitedUserId = existingUser.id;
// if (existingUser) {
// invitedUserId = existingUser.id;
const magicLink = await ctx.supabaseClient.auth.admin.generateLink({
type: "magiclink",
email: input.email,
options: {
redirectTo: process.env.WEBSITE_URL,
},
});
// 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 = 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;
// hashedToken = invite.data.properties?.hashed_token;
// verificationType = invite.data.properties?.verification_type;
const invitedUserAuthId = invite.data.user?.id;
const invitedUserEmail = invite.data.user?.email;
// 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,
},
});
// 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,
});
// 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",
});
// if (!newUser)
// throw new TRPCError({
// message: `Failed to create a new user for email ${invitedUserEmail}`,
// code: "INTERNAL_SERVER_ERROR",
// });
invitedUserId = newUser.id;
}
}
// invitedUserId = newUser.id;
// }
// }
if (!invitedUserId)
throw new TRPCError({