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

@@ -61,7 +61,6 @@ function fillEmptySpotsInTimeline(
clonedStartDate.getTime() <= clonedEndDate.getTime()
) {
if (prev === clonedStartDate.getTime()) {
console.log('GET OUT NOW!');
break;
}
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 superjson from 'superjson';
import { ZodError } from 'zod';
interface CreateContextOptions {
session: ReturnType<typeof auth> | null;
session: ReturnType<typeof getAuth> | null;
}
const t = initTRPC.context<CreateContextOptions>().create({
@@ -25,15 +26,24 @@ export const createTRPCRouter = t.router;
export const publicProcedure = t.procedure;
const enforceUserIsAuthed = t.middleware(({ ctx, next }) => {
const enforceUserIsAuthed = t.middleware(async ({ ctx, next }) => {
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);