try fix auth
This commit is contained in:
@@ -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}>
|
||||||
|
|||||||
@@ -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,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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(.*)',
|
||||||
|
],
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|||||||
@@ -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({
|
return next({
|
||||||
ctx: {
|
ctx: {
|
||||||
session: { ...ctx.session, user: ctx.session.user },
|
session: { ...ctx.session, user },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failes to get user', error);
|
||||||
|
throw new TRPCError({
|
||||||
|
code: 'UNAUTHORIZED',
|
||||||
|
message: 'Failed to get user',
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export const protectedProcedure = t.procedure.use(enforceUserIsAuthed);
|
export const protectedProcedure = t.procedure.use(enforceUserIsAuthed);
|
||||||
|
|||||||
Reference in New Issue
Block a user