UploadThing - Cant pass userId
Hey guys, my lack of knowledge is showing here 😓
I am trying to pass the input prop to pass my user ID but I cant use that prop since I am not really using the base UploadComponent as I followed the nextJS docs and it uses a generator. So it says input is not a valid prop, how do I fix this?
/api/uploadthing/core.ts
page.tsx
10 Replies
I don't think you should be passing
userId
from the client. If it's passed from the client, it can be modified. The outFileRouter
runs on the server, so you should get the userId
from whatever auth solution you're using.
For example, I'm using clerk, and my middleware looks like this:
I use auth0 for auth, they dont have an easy way to get ID the way we need it, so my Java server passes it instead via REST. . So your saying I should make a nextjs server action to get userId and call that server action from within the middleware?
Also thanks for your help 🙂
How is your auth implemented on the next.js side?
Sorry missed noto. No logic is really done on the system, so we only really have auth logic for the middleware to check if signed in. All logic is done on our Springboot server, so our NextJS app just forwards all requests there with the users token
So, if I understand it correctly, you have jwt token attached to your headers? If yes, you could get the token from headers in middleware, verity its signature, and hopefully get the
userId
from there.
You could technically send a request from the middleware to your java server, but that's extra latency + request you don't need.Decoding the token is a no go for us b/c auth0 is weird with the settings for that.
I know its probably not the best to do, but I would like to make the request in the middleware to get that if I cant pass it from frontend
I tried that but when I fetched it was like the request (from the /api/uploadthing/core.ts file) was not part of the system as /api/route threw an error saying the URL needed a prefix and the token was null, which is why it feels like its not "a part of" the system
Would a server action for the request fix this? So I would call the server action in the middleware?
Man, tbh, I don't know. Maybe? I realistically don't see a reason why a fetch request would fail. It feels like the configuration you have going is pretty strange.
Can you share your next.js middleware with me?
I feel very scared that this is bad haha
console.log
return value from headers()
in the ourFileRouter
middleware, what do you get?headers
function from import { headers } from "next/headers";
user
might be null
if you're not authenticated. I don't know what are the exact types. But something like this should work according to auth0 docs.Property 'user' does not exist on type 'Session | null | undefined'.
Figured it out, you really helped me get there, thanks!
In case someone else finds this helpful
Upload is succeeding except the webhook, maybe thats it hmm.