✅ JWT AUTH HELP. Postman Get Request Works. But when using the api in the frontend it doesnt
Hi, can anyone here help me? when i use JWT authentication in postman it works but when i use it on my react frontend it doesnt work and just returns unauthorized. been stuck with this error for 3 days and i really need help figuring this out.
50 Replies
how are you making the request from the frontend? show code.
then i use useeffect to run it
Why axios in todays day and age?
and the exact same token works in postman?
Surely
fetch
would work just fine?
yes exact same token i get from logging in works in postman
i could try it with fetch
hm, can you inspect the request in the browser network tab?
make sure the headers are there etc

okay good
Well, somehow the requests are not identical, thats all I can tell you for sure
if it works in postman, your backend works. It might be a whitespace somewhere or something
been looking at my code seems all okay to mee
i tried fetch and same thing

oh wait no
hold on

no yea still error
it says type:cors
so might be a preflight not working
cors should come before auth
well, in the UseCors part
AddCors doesn't matter

yeah i mean i dont think theres anythijng else really but i just cant seem to see the problem
different ports
what do you mean?
5015 vs 5173
5173 is my frontend
5015 is my backend
could start with an allow-all policy and see if that work, then add stuff back and see when it breaks
can do that
should i just delete options then?
.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials()
sadly still didnt work. this is frustrating
check the network tab, it might provide more info

i changed my frontend to 5174 so
this everything?
yeap
basically im signing in. getting my token
passing that token into my backend to get my tenant information
UseRouting?
but it seems to be harder t do
let me add that
nope
still the same
make sure the cors status is not cached, it doesn't seem to perform an options request
how can i achieve that?
usually some options in the dev tools to not use cache
chrome version

yeah. same things same errors
but ill maybe jsut go ahead and sleep this off. might just redo the whole thing again tomorrow and see where that leads me. do you know a good web api + JWT authentication video that is easy to understand?
sorry, i don't do frontend, just know the basics
no problem bro! thanks for helping me @Sehra @Pobiega i will succeed this tomorrow. or be super burnt out from it. but thanks guys
I'm fairly sure its a CORS error
ie, no tutorial for api + jwt will fix it
since thats not the issue
and postman is not a browser, it doesn't care about cors policies
exactly
okay ill see how i can check for CORS error tomorrow but for now i need to rest cause ive been at it for a couple of hours
sure
Is your port correct
It says 5173 on your c# app but the request here says 5174 on origin
5173 here
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
ive changed it to 5174 but same thing happened i still get unauthorized
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View