betterauth/react acting weird and hono cors problem

So I saw another post with hono and react but I still get the CORS problem. Tried everything I saw. Context: I'm running everything from an ubuntu server on my lan, 192.168.18.163. Backend is on hono port 3000 and frontend with vite and 5173. As both are there, on my code I write localhost because they are on the same location. I added all cors stuff I saw, proxy redirect from vite, even both ports just to see if that's the problem. And I still get cors problems. but the client seems to be acting weird (?) with the request because I can see the origin with my server ip, but should be localhost? here's the raw request:
OPTIONS /api/auth/sign-in/social undefined
Host: localhost:3000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:136.0) Gecko/20100101 Firefox/136.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br, zstd
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Referer: http://192.168.18.163:5173/
Origin: http://192.168.18.163:5173
Sec-GPC: 1
Connection: keep-alive
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: cross-site
OPTIONS /api/auth/sign-in/social undefined
Host: localhost:3000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:136.0) Gecko/20100101 Firefox/136.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br, zstd
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Referer: http://192.168.18.163:5173/
Origin: http://192.168.18.163:5173
Sec-GPC: 1
Connection: keep-alive
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: cross-site
And here u can see that I (should) have correctly added cors management (added both ports just in case idk), one screenshot is from server and the other from client and vite client conf:
No description
No description
No description
4 Replies
varo
varoOP2w ago
also other requests with tanstack query are working. It's only when better-auth does the request
bekacru
bekacru2w ago
does requests to better auth routes using tanstack query working? like sign-in
varo
varoOP2w ago
still the same
nktnet
nktnet2w ago
@varo your issue is probably the origin array in your first image For example, you have a single string:
origin: ["http://localhost:5173, http://localhost:3000"]
origin: ["http://localhost:5173, http://localhost:3000"]
when it should be separate strings (not stuck together in the same quote):
origin: ["http://localhost:5173", "http://localhost:3000"]
origin: ["http://localhost:5173", "http://localhost:3000"]
Also, there's no reason to specify the backend in trustedOrigin, so really you should just use
origin: ["http://localhost:5173"]
origin: ["http://localhost:5173"]
Cors work fine for me, same setup with - React frontend (vite, tanstack router) - Hono backend (nodejs) Full repo here for your reference: https://github.com/nktnet1/rt-stack

Did you find this page helpful?