pingdotgg / uploadthing

File uploads for modern web devs
https://uploadthing.com
MIT License
4.19k stars 312 forks source link

[Bug]: Undefined in Middleware #233

Closed oga-sven closed 1 year ago

oga-sven commented 1 year ago

What happened?

When trying to upload an image, i get an error that an imported util is not defined. I tried other solutions and imports but they get the same error. none of the imported functions work

What did you expect to happen?

I expected my utils to be defined

Version

"uploadthing": "^5.2.1"

Reproduction Steps

Below i have pasted my middleware. Without the auth stuff it works perfectly fine, I am using pnpm. Im not completely sure what to provide more than the middleware as its not something very specific. No matter what i import, be it a custom function from a different file or something from a library, i always get undefined.
This is only the case for imports, as same file functions work. more in the code below

Reproduction Repository

import { createUploadthing, type FileRouter } from 'uploadthing/next-legacy';
import permissionCheck from '../utils/nextAuth/permissionCheck';
import getAuthToken from '../utils/nextAuth/getAuthToken';
// import { getSession } from 'next-auth/react'; // Commented here. But this also returned undefined

const f = createUploadthing();

const sameFileTest = (testLog) => {
  console.log(testLog);
};

export const ourFileRouter = {
  imageUploader: f({ image: { maxFileSize: '4MB' } })
    .middleware(async ({ req, res }) => {
      sameFileTest('testing') //This works without a problem
      const token = await getAuthToken(req); // Its saying that this is undefined. 

      if (!token) throw new Error('Not authenticated');

      const response = await permissionCheck(token as string, 500);
      if (!response) throw new Error('Unauthorized');

      return { userId: 'userid' };
    })
    .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;

Relevant log output

ReferenceError: getAuthToken is not defined
    at Object.eval [as middleware] (webpack-internal:///(api)/./server/uploadthing.ts:19:23)      
    at eval (webpack-internal:///(api)/./node_modules/.pnpm/uploadthing@5.2.1_zod@3.21.4/node_modules/uploadthing/dist/chunk-UNVWQBTP.mjs:330:46)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async eval (webpack-internal:///(api)/./node_modules/.pnpm/uploadthing@5.2.1_zod@3.21.4/node_modules/uploadthing/dist/next-legacy.mjs:33:22)
    at async Object.apiResolver (Z:\projects\Oga Fitness\teamBicep\tb-home-2\node_modules\.pnpm\next@13.0.5_biqbaboplfbrettd7655fr4n2y\node_modules\next\dist\server\api-utils\node.js:363:9)       
    at async DevServer.runApi (Z:\projects\Oga Fitness\teamBicep\tb-home-2\node_modules\.pnpm\next@13.0.5_biqbaboplfbrettd7655fr4n2y\node_modules\next\dist\server\next-server.js:474:9)
    at async Object.fn (Z:\projects\Oga Fitness\teamBicep\tb-home-2\node_modules\.pnpm\next@13.0.5_biqbaboplfbrettd7655fr4n2y\node_modules\next\dist\server\next-server.js:736:37)
    at async Router.execute (Z:\projects\Oga Fitness\teamBicep\tb-home-2\node_modules\.pnpm\next@13.0.5_biqbaboplfbrettd7655fr4n2y\node_modules\next\dist\server\router.js:252:36)
    at async DevServer.run (Z:\projects\Oga Fitness\teamBicep\tb-home-2\node_modules\.pnpm\next@13.0.5_biqbaboplfbrettd7655fr4n2y\node_modules\next\dist\server\base-server.js:383:29)
    at async DevServer.run (Z:\projects\Oga Fitness\teamBicep\tb-home-2\node_modules\.pnpm\next@13.0.5_biqbaboplfbrettd7655fr4n2y\node_modules\next\dist\server\dev\next-dev-server.js:734:20)      
    at async DevServer.handleRequest (Z:\projects\Oga Fitness\teamBicep\tb-home-2\node_modules\.pnpm\next@13.0.5_biqbaboplfbrettd7655fr4n2y\node_modules\next\dist\server\base-server.js:321:20)  


### Code of Conduct

- [X] I agree to follow this project's Code of Conduct
oga-sven commented 1 year ago

Fyi i did now fix it by splitting the auth into a local function and letting that do the logic like the docs. But it seems weird to me that this is the intended way

juliusmarminge commented 1 year ago

Reproduction repo plzzz

juliusmarminge commented 1 year ago

Please reopen with a reproduction repo if the issue persists