How to log in from the server
Hello,
I have a login page with a form and I'm trying to signIn the user on the server:
I get the
success: true
, but when I navigate on another page I do
But session
is null, what should I do to get the session or cookie created?
Thank you10 Replies
you can integrate the client library of better-auth. Instead of doing auth.api.signInEmail, you can create a client instance and do authClient.signIn.email({
email: data.email,
password: data.password,
callbackURL: "/",
});
Thank you, I know but I'm trying to use the server side because I do operations in a formAction, I tried client side and it kind of works, I'd like to know how to do it server side.
That won't be setting a session cookie, so your next navigation doesn't know about the session. Try passing headers:request.headers as an option.
headers : await headers()
You mean:
?
yes
import headers from next/navigation
yeah the thing you are running the function and make sure it sending the correct setCookie headers and you can levarage more if you enabled cookieCache to not be able to ping the db for getting the session
Looks like you're using sveltekit? If so, just add headers:request.headers as an option to the signInEmail config in the form action.
Yes I'm using SvelteKit,
You mean like this?
Yes.