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
Ping
Ping3mo ago
Are you able to do the signinEmail from the client instead? Most cases that's how it's intended.
Emirhan Güneç
Emirhan GüneçOP3mo ago
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?
Ping
Ping3mo ago
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.
Emirhan Güneç
Emirhan GüneçOP3mo ago
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
bekacru
bekacru3mo ago
could you share your auth config?
Emirhan Güneç
Emirhan GüneçOP3mo ago
of course
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { nextCookies } from "better-auth/next-js";

import { db } from "@influencer/db/client";

export type Session = typeof auth.$Infer.Session;

export const auth = betterAuth({
emailAndPassword: {
enabled: true,
},
database: drizzleAdapter(db, {
provider: "pg",
}),
plugins: [nextCookies()],
});
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { nextCookies } from "better-auth/next-js";

import { db } from "@influencer/db/client";

export type Session = typeof auth.$Infer.Session;

export const auth = betterAuth({
emailAndPassword: {
enabled: true,
},
database: drizzleAdapter(db, {
provider: "pg",
}),
plugins: [nextCookies()],
});
Ping
Ping3mo ago
And you are using server actions, right?
Emirhan Güneç
Emirhan GüneçOP3mo ago
yep, i tried in RSC, and server actions with RSC
bekacru
bekacru3mo ago
in RSC cookies can't be set. It needs to be from a server action you call.
Ping
Ping3mo ago
Can we see your server actions code?
Emirhan Güneç
Emirhan GüneçOP3mo ago
actions.ts
"use server";

import { headers } from "next/headers";

import { auth } from "@influencer/auth";

export async function login({
email,
password,
}: {
email: string;
password: string;
}) {
return await auth.api.signInEmail({
headers: await headers(),
body: {
email,
password,
},
});
}
"use server";

import { headers } from "next/headers";

import { auth } from "@influencer/auth";

export async function login({
email,
password,
}: {
email: string;
password: string;
}) {
return await auth.api.signInEmail({
headers: await headers(),
body: {
email,
password,
},
});
}
so i should use server action from client component, not from RSC, right?
bekacru
bekacru3mo ago
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
Emirhan Güneç
Emirhan GüneçOP3mo ago
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
Ping
Ping3mo ago
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
Emirhan Güneç
Emirhan GüneçOP3mo ago
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
Ping
Ping3mo ago
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...
Ping
Ping3mo ago
How it works now:
No description
Ping
Ping3mo ago
How it would work if you want your idea to work:
No description
Ping
Ping3mo ago
Besides that, it's a good attempt at an idea though.
Emirhan Güneç
Emirhan GüneçOP3mo ago
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
Ping
Ping3mo ago
That might possible. 🤔
Emirhan Güneç
Emirhan GüneçOP3mo ago
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 😦
Ping
Ping3mo ago
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.
Emirhan Güneç
Emirhan GüneçOP3mo ago
maybe it will be a plugin, so users can know this behaviour is different than general
Ping
Ping3mo ago
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
Emirhan Güneç
Emirhan GüneçOP3mo ago
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 👏
Ping
Ping3mo ago
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...
Emirhan Güneç
Emirhan GüneçOP3mo ago
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...
Ping
Ping3mo ago
Is there any reason why you don't want to use the authClient?
Emirhan Güneç
Emirhan GüneçOP3mo ago
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
daveycodez
daveycodez3mo ago
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.
Ping
Ping3mo ago
Have you started work on the docs yet?
daveycodez
daveycodez3mo ago
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
Ping
Ping3mo ago
Sounds good.
daveycodez
daveycodez3mo ago
Goal atm is to have 2.0 ready this weekend then start on Fumadocs
Emirhan Güneç
Emirhan GüneçOP3mo ago
sounds cool, im defintely checking it out 👍

Did you find this page helpful?