try fix auth
This commit is contained in:
@@ -61,7 +61,6 @@ function fillEmptySpotsInTimeline(
|
||||
clonedStartDate.getTime() <= clonedEndDate.getTime()
|
||||
) {
|
||||
if (prev === clonedStartDate.getTime()) {
|
||||
console.log('GET OUT NOW!');
|
||||
break;
|
||||
}
|
||||
prev = clonedStartDate.getTime();
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user