adrianhajdin / threads

Develop Threads, Next.js 13 app that skyrocketed to 100 million sign-ups in less than 5 days, and dethroned giants like Twitter, ChatGPT, and TikTok to become the fastest-growing app ever!
https://threads-psi.vercel.app
1.47k stars 279 forks source link

UploadThing Middleware failed to run #60

Open saurabhbhise16 opened 1 year ago

saurabhbhise16 commented 1 year ago
import { currentUser } from "@clerk/nextjs";
import { createUploadthing, type FileRouter } from "uploadthing/next";

const f = createUploadthing();

const getUser = async () => await currentUser();

export const ourFileRouter = {
    // Define as many FileRoutes as you like, each with a unique routeSlug
    media: f({ image: { maxFileSize: "4MB", maxFileCount: 1 } })
        // Set permissions and file types for this FileRoute
        .middleware(async (req) => {
            // This code runs on your server before upload
            const user = await getUser();

            // If you throw, the user will not be able to upload
            if (!user) throw new Error("Unauthorized");

            // Whatever is returned here is accessible in onUploadComplete as `metadata`
            return { userId: user.id };
        })
        .onUploadComplete(async ({ metadata, file }) => {
            // This code RUNS ON YOUR SERVER after upload
            console.log("Upload complete for userId:", metadata.userId);

            console.log("file url", file.url);
        }),
} satisfies FileRouter;

export type OurFileRouter = typeof ourFileRouter;

I am at halfway and realised that onboarding page is showing this error and thisis my third trial

saurabhbhise16 commented 1 year ago

Please let me know if you need more details

Ahmad-Dorri commented 1 year ago

hello, I think you should use const {user} = await getUser(); instead of const user = await getUser(); Let me know if it helps you.

Reedam-Ranjan commented 12 months ago

@saurabhbhise16 Have you properly imported the uploadthing secret keys in the .env file ? Also have a check that if you have exported the GET & POST methods in the route.ts file.

saurabhbhise16 commented 12 months ago

@Reedam-Ranjan @Ahmad-Dorri thank you for interest, sure I will make changes and let you guys know