Session is null inside of a server action when calling it from the uploadthing router

/api/uploadthing/core.ts
articleCoverUploader: f({
image: { maxFileCount: 1, maxFileSize: "2MB" },
})
.input(
z.object({
title: z.string().trim().min(1),
tag: z.string().trim().min(1),
}),
)
.middleware(async ({ input }) => {
const session = await getServerAuthSession();

if (session?.user.role === "USER") {
throw new Error("Permission denied!");
}

return { input };
})
.onUploadComplete(async ({ file, metadata: { input } }) => {
await createArticle({
...input,
coverImage: file.url,
});
}),
articleCoverUploader: f({
image: { maxFileCount: 1, maxFileSize: "2MB" },
})
.input(
z.object({
title: z.string().trim().min(1),
tag: z.string().trim().min(1),
}),
)
.middleware(async ({ input }) => {
const session = await getServerAuthSession();

if (session?.user.role === "USER") {
throw new Error("Permission denied!");
}

return { input };
})
.onUploadComplete(async ({ file, metadata: { input } }) => {
await createArticle({
...input,
coverImage: file.url,
});
}),
I'm using the next-safe-actions
export const protectedAction = createSafeActionClient({
async middleware() {
const session = await getServerAuthSession();

if (!session) {
throw new Error("Unauthorized!");
}

return { session };
},
});
export const protectedAction = createSafeActionClient({
async middleware() {
const session = await getServerAuthSession();

if (!session) {
throw new Error("Unauthorized!");
}

return { session };
},
});
When I call the server action inside of a page, the session is not null, but when I try to create the article inside of a uploadthing router the session is null? How I can solve it? Any recommendations?
0 Replies
No replies yetBe the first to reply to this messageJoin