Using secure cookies on local dev server (running https) not working
I am need to test my application locally using https for a third party integration (unrelated to this), but I am running into an issue where my sessions are not being found after sign-in when using a local https server, using secure tokens. I tried to set the advanced useSecureCookies config to true, but that did not work. The secure cookies are being set in my browser, the sessions exist in the database, and the tokens that I am logging on the console are all lining up, however when I use the getSession hook, its returning null.
Debuggin items I have checked:
- BETTER_AUTH_URL is being set to https://localhost:3000
- Https is working
- Tried setting useSecureCookies in the advanced config section
- Tried setting the cookiePrefix to Secure-better-auth
Example logging when logging out my session_token cookie before the getSession hook, and the response.
Logic for getting the session with set cookies:
const { getWebRequest } = await import('vinxi/http');
const request = getWebRequest();
const cookies = request?.headers.get('cookie') || '';
sessionResponse = await authClient.getSession({
fetchOptions: { headers: { cookie: cookies } }
});
[root] beforeLoad: Fetching session from authClient.getSession
[SSR] Raw cookie header: Secure-better-auth.session_token=<valid_token>.<signature>
[SSR] Parsed session ID: <valid_token>
[SSR] Parsed signature: <signature>
[root] Session fetch result: { data: null, error: { status: 0, statusText: '' } }
2 Replies
Hello
I also have issue with Secure cookie on https.
Have you solved this problem?
Hey, did either of you solve your issue ? I seem to be running into the same issue
I don't know if it was similar for you, but in my case, I was using a self-signed cert while waiting for some details to setup let's encrypt. This resulted in my calls to authClient.getSession failing silently and similarly to yours, (
result: { data: null, error: { status: 0, statusText: '' } }
)
I noticed this when i made calls with postman to the api/auth/get-session
and they worked.
I tested this by making some manual fetch()
calls inside my authentication middleware
Which gave me these logs:
I then tested by setting the environment variable NODE_TLS_REJECT_UNAUTHORIZED = 0
and things resolved.
I guess it would be nice if these errors bubbled up in better-auth (maybe they do as my library is a couple of months out of date)