try fix auth

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-02-17 13:13:52 +01:00
parent 743aa39036
commit e05d8bdca1
5 changed files with 30 additions and 17 deletions

View File

@@ -109,7 +109,6 @@ export default async function Page({
}), }),
getExists(organizationId, projectId), getExists(organizationId, projectId),
]); ]);
console.log(events[0]);
return ( return (
<PageLayout title="Events" organizationSlug={organizationId}> <PageLayout title="Events" organizationSlug={organizationId}>

View File

@@ -1,5 +1,6 @@
import { appRouter } from '@/server/api/root'; import { appRouter } from '@/server/api/root';
import { auth } from '@clerk/nextjs'; import { getSession } from '@/server/auth';
import { getAuth } from '@clerk/nextjs/server';
import { fetchRequestHandler } from '@trpc/server/adapters/fetch'; import { fetchRequestHandler } from '@trpc/server/adapters/fetch';
const handler = (req: Request) => const handler = (req: Request) =>
@@ -7,12 +8,10 @@ const handler = (req: Request) =>
endpoint: '/api/trpc', endpoint: '/api/trpc',
req, req,
router: appRouter, router: appRouter,
createContext: () => { async createContext({ req }) {
console.log('------- createContext --------'); console.log('------- createContext --------');
const session = auth(); const session = getAuth(req as any);
console.log('session', session);
console.log('session', JSON.stringify(session, null, 2)); console.log('session', JSON.stringify(session, null, 2));
return { return {
session, session,
}; };

View File

@@ -8,5 +8,11 @@ export default authMiddleware({
}); });
export const config = { export const config = {
matcher: ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api)(.*)'], matcher: [
'/((?!.+\\.[\\w]+$|_next).*)',
'/',
'/(api)(.*)',
'/(api|trpc)(.*)',
'/api/trpc(.*)',
],
}; };

View File

@@ -61,7 +61,6 @@ function fillEmptySpotsInTimeline(
clonedStartDate.getTime() <= clonedEndDate.getTime() clonedStartDate.getTime() <= clonedEndDate.getTime()
) { ) {
if (prev === clonedStartDate.getTime()) { if (prev === clonedStartDate.getTime()) {
console.log('GET OUT NOW!');
break; break;
} }
prev = clonedStartDate.getTime(); prev = clonedStartDate.getTime();

View File

@@ -1,10 +1,11 @@
import type { auth } from '@clerk/nextjs'; import { clerkClient } from '@clerk/nextjs';
import type { getAuth } from '@clerk/nextjs/server';
import { initTRPC, TRPCError } from '@trpc/server'; import { initTRPC, TRPCError } from '@trpc/server';
import superjson from 'superjson'; import superjson from 'superjson';
import { ZodError } from 'zod'; import { ZodError } from 'zod';
interface CreateContextOptions { interface CreateContextOptions {
session: ReturnType<typeof auth> | null; session: ReturnType<typeof getAuth> | null;
} }
const t = initTRPC.context<CreateContextOptions>().create({ const t = initTRPC.context<CreateContextOptions>().create({
@@ -25,15 +26,24 @@ export const createTRPCRouter = t.router;
export const publicProcedure = t.procedure; export const publicProcedure = t.procedure;
const enforceUserIsAuthed = t.middleware(({ ctx, next }) => { const enforceUserIsAuthed = t.middleware(async ({ ctx, next }) => {
if (!ctx.session?.userId) { if (!ctx.session?.userId) {
throw new TRPCError({ code: 'UNAUTHORIZED' }); throw new TRPCError({ code: 'UNAUTHORIZED', message: 'Not authenticated' });
}
try {
const user = await clerkClient.users.getUser(ctx.session.userId);
return next({
ctx: {
session: { ...ctx.session, user },
},
});
} catch (error) {
console.error('Failes to get user', error);
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'Failed to get user',
});
} }
return next({
ctx: {
session: { ...ctx.session, user: ctx.session.user },
},
});
}); });
export const protectedProcedure = t.procedure.use(enforceUserIsAuthed); export const protectedProcedure = t.procedure.use(enforceUserIsAuthed);