SignIn doesn't work server side
I think I have an issue related headers or cookies. I'm using nextjs 15, authClient works flawlessly, also server-side getSession works fine too. but when i try to auth.api.signInEmail(), it creates a new session to db, but not returning session on browser, even after refresh. Any Ideas?
36 Replies
Are you able to do the signinEmail from the client instead?
Most cases that's how it's intended.
yes, i can use signInEmail from client, so it means i can't use RSC on my signIn page, right?
i think it can be related nextCookies plugin? do you have any ideas to fix this?
Oh, you might be right about that.
The only thing that comes to mind is that the nextCookies plugin needs to be at the end of the plugin array.
yes it is, it is the single plugin i use
firstly i think it's related to nextjs 15, but i tried same project with clean nextj14, results are same. Nextjs also has backwards compability of asynchrounous hedaers/cookies so i eliminated this possibility
could you share your auth config?
of course
And you are using server actions, right?
yep, i tried in RSC, and server actions with RSC
in RSC cookies can't be set. It needs to be from a server action you call.
Can we see your server actions code?
actions.ts
so i should use server action from client component, not from RSC, right?
not exactly, If you call this from RSC it should work as well. It's just if you're trying to set cookies while RSC is streaming it's not possible. If you're calling this and it's not setting cookies, let me know what version of better auth you're in. Could be a bug
im using ^1.1.14 version
i haven't looked at how nextCookies works but i think i have an idea.
nextCookies should try to set cookies in middleware, middleware allows setting cookies. If nextCookies uses middleware, cookies will be set on RSC, client components, routers, basicly all things runs after from middleware
I'm not sure using the Next middleware would be the best idea for some reasons.
But regardless of that, I'm not sure how the BetterAuth API in RSC could signal to the next middleware to append cookie info
I am curious about why isn't ideal, I'm new to auth things, but you could add a step to installation to add middleware from better auth, and this middleware can control cookies, right?
maybe it can be a plugin too, it can be added into auth config and users exports a middleware function imported from plugin in middleware.ts
Putting aside my opinion that next middleware isn't the best idea for varying reasons.
I'm not sure how we would go about having an API from BetterAuth being called in an RSC context, for the Next Middleware to know to add cookies to a request.
Not to mention that RSC would be called after the middleware has been called, so if there was an API called in RSC, the middleware wouldn't be there to add the cookies...
How it works now:

How it would work if you want your idea to work:

Besides that, it's a good attempt at an idea though.
yeah, I know understand well this stuff beyonds, me 🙂
can we signal from RSC, after the page reloads or when next time middleware invokes, then we set cookies? Generally all logins/signups ends with full page reload
sorry for my english btw, im not native speaker
That might possible. 🤔
but from my knowledge, better auth is working currently instant, without needing any refresh, if my solution became a part of better-auth, it will break this 😦
Not exactly.
If Better Auth wanted this idea, it would be implemented separately with different function names/methods.
And also very much warned in the docs saying that this is probably not ideal.
maybe it will be a plugin, so users can know this behaviour is different than general
Yeah maybe.
It would be really hard to implement though.
Because if assigning a cookie is only avaliable in the middleware, than you'll need a DB to know when to assign a cookie, and to who.
Since the RSC can't communicate to the middleware
yeah, you're right
what you suggest to me, if i persist to use RSC, how can i workaround this basically? or it will be worthed?
btw, better auth is fantastic library and has great community, keep up with the good work 👏
If I'm being honest, I don't think it's worth it...
With your server actions code, is your component a server component?
Because that might be the problem, not 100% though...
if i use server action in client component it works, but it's basically bad way of using authClient, when you try to use server action on RSC, it will be cookie issue
in idea, i want to use auth in tRPC router, and use this router, but i think it will be more complicated than just use authClient.signIn.email...
Is there any reason why you don't want to use the authClient?
my project is a monorepo project which will be have 2-3 nextjs app, 1 expo app and 10+ packages, i want my logic in shared. If you have suggestions for this besides on better auth, I'would love to hear
I did make a better-auth-ui package mainly for sharing my auth pages code across all my sites https://github.com/daveyplate/better-auth-ui
GitHub
GitHub - daveyplate/better-auth-ui: Premade shadcn better-auth card...
Premade shadcn better-auth cards & forms. Contribute to daveyplate/better-auth-ui development by creating an account on GitHub.
Have you started work on the docs yet?
I'm finishing Neon Drizzle Tanstack library atm, need to make sure everything is working well there with better-auth, do a full test then I'm going back to better-auth-ui updates
Sounds good.
Goal atm is to have 2.0 ready this weekend then start on Fumadocs
sounds cool, im defintely checking it out 👍