FRE-588: Complete tRPC/Clerk integration with database schema updates
- Updated router.ts middleware for Clerk authentication - Modified test contexts to use clerkUserId - Added team tables to test schema - Updated WaitlistForm and waitlist page - Created src/server/trpc/ parallel structure All 258 tests pass. Ready for Security Reviewer.
This commit is contained in:
@@ -15,12 +15,24 @@ const isAuthenticated = t.middleware(({ ctx, next }) => {
|
||||
return next({ ctx: { ...ctx, clerkUserId: ctx.clerkUserId } });
|
||||
});
|
||||
|
||||
// Middleware for database access
|
||||
const hasDb = t.middleware(({ ctx, next }) => {
|
||||
// Middleware for database access and user lookup
|
||||
const hasDb = t.middleware(async ({ ctx, next }) => {
|
||||
if (!ctx.db) {
|
||||
throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', message: 'Database not available' });
|
||||
}
|
||||
return next({ ctx: { ...ctx, db: ctx.db } });
|
||||
|
||||
let userId: number | undefined;
|
||||
if (ctx.clerkUserId) {
|
||||
const { users } = await import('../../src/db/schema');
|
||||
const userRows = await ctx.db.select({ id: users.id })
|
||||
.from(users)
|
||||
.where(eq(users.clerkId, ctx.clerkUserId));
|
||||
if (userRows.length > 0) {
|
||||
userId = userRows[0].id;
|
||||
}
|
||||
}
|
||||
|
||||
return next({ ctx: { ...ctx, db: ctx.db, userId } });
|
||||
});
|
||||
|
||||
// Middleware for project ownership verification
|
||||
@@ -35,7 +47,7 @@ const hasProjectAccess = t.middleware(async ({ ctx, next }) => {
|
||||
throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', message: 'Database not available' });
|
||||
}
|
||||
const { users } = await import('../../src/db/schema');
|
||||
const userRows = await ctx.db.select({ dbId: users.id, clerkId: users.clerkId })
|
||||
const userRows = await ctx.db.select({ id: users.id, clerkId: users.clerkId })
|
||||
.from(users)
|
||||
.where(eq(users.clerkId, ctx.clerkUserId));
|
||||
const dbUser = userRows[0];
|
||||
@@ -49,10 +61,10 @@ const hasProjectAccess = t.middleware(async ({ ctx, next }) => {
|
||||
if (!project) {
|
||||
throw new TRPCError({ code: 'NOT_FOUND', message: `Project ${ctx.projectId} not found` });
|
||||
}
|
||||
if (project.ownerId !== dbUser.dbId) {
|
||||
if (project.ownerId !== dbUser.id) {
|
||||
throw new TRPCError({ code: 'FORBIDDEN', message: `You do not have access to project ${ctx.projectId}` });
|
||||
}
|
||||
return next({ ctx: { ...ctx, projectId: ctx.projectId, userId: dbUser.dbId } });
|
||||
return next({ ctx: { ...ctx, projectId: ctx.projectId, userId: dbUser.id } });
|
||||
});
|
||||
|
||||
// Base router
|
||||
|
||||
Reference in New Issue
Block a user