refactor: remove supabase auth
This commit is contained in:
@@ -2,12 +2,16 @@ import type { inferRouterInputs, inferRouterOutputs } from "@trpc/server";
|
||||
|
||||
import type { AppRouter } from "./root";
|
||||
import { appRouter } from "./root";
|
||||
import { createCallerFactory, createTRPCContext } from "./trpc";
|
||||
import {
|
||||
createCallerFactory,
|
||||
createNextApiContext,
|
||||
createTRPCContext,
|
||||
} from "./trpc";
|
||||
|
||||
const createCaller = createCallerFactory(appRouter);
|
||||
|
||||
type RouterInputs = inferRouterInputs<AppRouter>;
|
||||
type RouterOutputs = inferRouterOutputs<AppRouter>;
|
||||
|
||||
export { createTRPCContext, appRouter, createCaller };
|
||||
export { createTRPCContext, appRouter, createCaller, createNextApiContext };
|
||||
export type { AppRouter, RouterInputs, RouterOutputs };
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { authRouter } from "./routers/auth";
|
||||
import { boardRouter } from "./routers/board";
|
||||
import { cardRouter } from "./routers/card";
|
||||
import { feedbackRouter } from "./routers/feedback";
|
||||
@@ -11,7 +10,6 @@ import { workspaceRouter } from "./routers/workspace";
|
||||
import { createTRPCRouter } from "./trpc";
|
||||
|
||||
export const appRouter = createTRPCRouter({
|
||||
auth: authRouter,
|
||||
board: boardRouter,
|
||||
card: cardRouter,
|
||||
feedback: feedbackRouter,
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { z } from "zod";
|
||||
|
||||
import { createTRPCRouter, publicProcedure } from "../trpc";
|
||||
|
||||
export const authRouter = createTRPCRouter({
|
||||
loginWithEmail: publicProcedure
|
||||
.meta({
|
||||
openapi: {
|
||||
method: "POST",
|
||||
path: "/auth/login/email",
|
||||
summary: "Login with email",
|
||||
description: "Sends a login URL to the provided email address",
|
||||
tags: ["Auth"],
|
||||
},
|
||||
})
|
||||
.input(z.object({ email: z.string() }))
|
||||
.output(z.object({ success: z.boolean() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const { data } = await ctx.supabaseClient.auth.signInWithOtp({
|
||||
email: input.email,
|
||||
options: {
|
||||
emailRedirectTo: `${process.env.WEBSITE_URL}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!data)
|
||||
throw new TRPCError({
|
||||
message: `Failed to login with email`,
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
}),
|
||||
loginWithOAuth: publicProcedure
|
||||
.meta({
|
||||
openapi: {
|
||||
method: "POST",
|
||||
path: "/auth/login/oauth",
|
||||
summary: "Login with OAuth",
|
||||
description:
|
||||
"Initiates the login process for a user with the given OAuth provider",
|
||||
tags: ["Auth"],
|
||||
},
|
||||
})
|
||||
.input(z.object({ provider: z.string() }))
|
||||
.output(z.object({ url: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
if (input.provider !== "google")
|
||||
throw new TRPCError({
|
||||
message: `Unsupported OAuth provider: ${input.provider}`,
|
||||
code: "BAD_REQUEST",
|
||||
});
|
||||
|
||||
const { data } = await ctx.supabaseClient.auth.signInWithOAuth({
|
||||
provider: "google",
|
||||
options: {
|
||||
queryParams: {
|
||||
access_type: "offline",
|
||||
prompt: "consent",
|
||||
},
|
||||
redirectTo: `${process.env.WEBSITE_URL}/api/auth/confirm`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!data.url)
|
||||
throw new TRPCError({
|
||||
message: `Failed to login with OAuth`,
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
});
|
||||
|
||||
return { url: data.url };
|
||||
}),
|
||||
});
|
||||
@@ -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({
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { FetchCreateContextFnOptions } from "@trpc/server/adapters/fetch";
|
||||
import type { CreateNextContextOptions } from "@trpc/server/adapters/next";
|
||||
import type { NextRequest } from "next/server";
|
||||
import type { OpenApiMeta } from "trpc-to-openapi";
|
||||
import { initTRPC, TRPCError } from "@trpc/server";
|
||||
import superjson from "superjson";
|
||||
@@ -44,6 +45,16 @@ export const createTRPCContext = async ({
|
||||
return createInnerTRPCContext({ db, user: session?.user });
|
||||
};
|
||||
|
||||
export const createNextApiContext = async (req: NextRequest) => {
|
||||
const session = await auth.api.getSession({
|
||||
headers: req.headers,
|
||||
});
|
||||
|
||||
const db = createDrizzleClient();
|
||||
|
||||
return createInnerTRPCContext({ db, user: session?.user });
|
||||
};
|
||||
|
||||
export const createRESTContext = async ({ req }: CreateNextContextOptions) => {
|
||||
const authHeader = req.headers.authorization;
|
||||
const accessToken = authHeader?.startsWith("Bearer ")
|
||||
@@ -57,7 +68,8 @@ export const createRESTContext = async ({ req }: CreateNextContextOptions) => {
|
||||
}
|
||||
|
||||
const session = await auth.api.getSession({
|
||||
headers: req.headers,
|
||||
// @ts-expect-error
|
||||
headers: new Headers(req.headers),
|
||||
});
|
||||
|
||||
return createInnerTRPCContext({ db, user: session?.user });
|
||||
|
||||
Reference in New Issue
Block a user