Next Hono session is empty
I do not know how to structure this question to be honest, But is there a way to get the session details or another way to check if the user is logged in ? if the frontend and backend is hosted on seperate server ( ie: auth.ts file is not available at next frontend side).
in the image:
1. backend is the hono application
2. users is the frontend nextjs application
What works:
Login is working , register user is working
What i need
get the session details of the logged in user.
Some template projects i saw in github uses hono as a replacement to next api route. That is not what i want . I want the hono to be a seperate server.


Solution:
Jump to solution
So to digest the issue was I was logging in from a server component which did not set the cookies in the nextjs application.
which would not be a problem if I was using hono as a replacement of the next api router, where nextCookies() plugin could be helping while the logging was happening from a server component.
but in my case i am not replacing the next router .. i am having a seperate hono server so i guess the plugin nextCookies would not reflect to the next server
...

23 Replies
Did you add the nextjs setCookie plugin? https://www.better-auth.com/docs/integrations/next and also specify which domains should have access to the cookie https://www.better-auth.com/docs/concepts/cookies#cross-subdomain-cookies
Next.js integration | Better Auth
Integrate Better Auth with Next.js.
Cookies | Better Auth
Learn how cookies are used in Better Auth.
well technically yes , this file is inside the hono project

i am using this authclient from nextjs front end to call the signin method
handlelogin function from frontend

add the advanced option to your auth config and replace the domain/trustedOrigins with your client url
hi
updated
still the session is null , this is how i getsession in my frontend right now
this is the authclient
try awaiting the authClient in the frontend
and try updating auth config
that actually did make a change
previously the session was an empty object
{}
now its {data:null,error:null}

Okay cool. Can you try logging in again?
well i did but not good , this ss is taken in a incognito window ... I will try by creating a new user

nop new user also returns data null and error null
Ok. My only other guess will be trying this. I see the server is returning a success but for whatever reason next isn't reading the cookie
funny thing is registration and login is actually working .. just cant get the session 😿
Does your fetch have credentials include on the client?
tried and failed, well i thought of that way too like since i am logging in using server action that happens on the server side and then next cant set the cookies since the auth.ts file is actually on a seperate project ( nextCookies() ) might not be working.
So I tried making the login request from a client component but that gave me the issue like you need to have Access-Control-Cross-Credentials set to true , but could not debug that issue ..
show your cors settings @aswnss
and as you said you should not use nextcookies in sepearte backend auth instance
this is the issue ... this type of architecture is used when we switch the next api with hono right .. that is not what i am trying to achieve .. in my mind i need to detach the frontend ... not just the frontend api routing

cors is set like this in hono

add those
did not solve the issue 😥 .. well let me try logging in from a client component now instead of the "use server" method
have you tried in incognito
what does the dev tools says
also when you are fetching on the server you shouldnt probably use the authclient
and when fetching in client use the authClient with credentials: "include"
also in the server you should use credentials include
this is very important
read the official integration guide carefully:
https://www.better-auth.com/docs/integrations/hono
this did fix it
Solution
So to digest the issue was I was logging in from a server component which did not set the cookies in the nextjs application.
which would not be a problem if I was using hono as a replacement of the next api router, where nextCookies() plugin could be helping while the logging was happening from a server component.
but in my case i am not replacing the next router .. i am having a seperate hono server so i guess the plugin nextCookies would not reflect to the next server
What fixed :
when i made the login request from a client component nextjs will automatically set the cookies when the header contains a set cookie value
Why it did not work previously:
The cors setting was needed to be added in the hono side which i did not do
