Sessions not working
I have Hono as my backend and Next as my frontend.
Hono and Next are on different codebases, I'm not using Hono in Next, I'm using it seperately.
These are the issues I'm facing:
1. When I logged in everything works as expected, however when I sign-out the session gets deleted in my database however the cookie stays and therefore I stay on the protected dashboard page
2. When I add secure cookies and/or add session caching, on login the session gets created in the backend but no cookie gets generated
13 Replies
Here is my hono auth.ts
.
And here is my frontend auth-client
and my middleware.ts
I even tried signing in and out using the
/api/auth/reference
, however in the cookies of the backend url as well, same thing happens.
1. Cookie stays
2. Getting session returns null
3. Signing our returns success but nothing happens in terms of deleting the cookie.
As for trying it with secure cookie or cached cookie.
1. Login success
2. Getting session = null
:0
is it on production or local?
for prod you need to congiure cross sub domain cookies or if it's across domains, you'd need to use something like a bearer token instead to aovid using cross domain cookies
Im not on production. Im currently on development, i created a simple project just to make sure everything is working fine for auth
Frontend (Nextjs): http://localhost:3000
Backend (Hono node): http://localhost:4000
- In my frontend i have simply email sign-in and up with a protected dashboard page with sign out button (I only have auth client on the frontend)
- In my backend i added the default settings/middleware/cors like in the documentation for Hono
My plan for later on is to add my backend to
api.mydomain.com
and my frontend to mydomain.com
as well as connecting an Expo app to my backend. But I still haven't reached that part yetremove
useSecureCookies
if you're on localI did, same result with cache cookie
And even without cache cookie, the cookies are not being deleted from the browser cookie
despite them being deleted from the database
cookie cache a separte concern
first make sure the session cookie is being set on your browser
you can disable cookie cache for the time being to get this working as well
in localhost, there should be no difference cookie wise between two ports
Without cache and secure cookies
1. The cookie gets set
2. On sign out it doesnt get deleted
that's why it doesn't require setting up a subdomain cookie
secure cookie shouldn't work locally at all
it requires
https
connectionYes i removed it 😅
cookie cache is just a caching layer. don't worry about it now. get the session token working first
I think i found the problem
It something I did and totally forgot about for Hono itself.
I've changed the way the Hono serves the application by bypassing some stuff so that my socket works
I'll figure out how to allow better-auth to bypass this as well
Solution
Marking this as solved as it's a problem made by me and not the library
I have a very similar set up and I either run into cors issues or my cookies don't get send to the backend. Is it recommended to have the hono server fully decoupled from my frontend (next.js) and still use sessions?