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:
export const actions = {
default: async ({cookies, request}) => {
const email = formData.get("email");
const password = formData.get("password");

try {
const response = await auth.api.signInEmail({
body: { email, password },
asResponse: true
});
return { success: true };
} catch (err) {
return fail(401, { error: 'Invalid credentials' });
}
export const actions = {
default: async ({cookies, request}) => {
const email = formData.get("email");
const password = formData.get("password");

try {
const response = await auth.api.signInEmail({
body: { email, password },
asResponse: true
});
return { success: true };
} catch (err) {
return fail(401, { error: 'Invalid credentials' });
}
I get the success: true, but when I navigate on another page I do
import { auth } from "$lib/auth";

export const load = async ({ request, locals }) => {
const session = await auth.api.getSession({
headers: request.headers
})

console.log(session)
import { auth } from "$lib/auth";

export const load = async ({ request, locals }) => {
const session = await auth.api.getSession({
headers: request.headers
})

console.log(session)
But session is null, what should I do to get the session or cookie created? Thank you
10 Replies
Ashwanee Kumar Gupta
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: "/", });
Filippo
FilippoOP23h ago
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.
Stuart B
Stuart B22h ago
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.
rhitune
rhitune21h ago
headers : await headers()
Filippo
FilippoOP20h ago
You mean:
import { auth } from "$lib/auth";

export const load = async ({ request, locals }) => {
const session = await auth.api.getSession({
headers: await headers()
})
import { auth } from "$lib/auth";

export const load = async ({ request, locals }) => {
const session = await auth.api.getSession({
headers: await headers()
})
?
rhitune
rhitune19h ago
yes import headers from next/navigation
KiNFiSH
KiNFiSH18h ago
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
Stuart B
Stuart B18h ago
Looks like you're using sveltekit? If so, just add headers:request.headers as an option to the signInEmail config in the form action.
Filippo
FilippoOP2h ago
Yes I'm using SvelteKit, You mean like this?
const response = await auth.api.signInEmail({
body: { email, password },
asResponse: true,
headers: request.headers
});
const response = await auth.api.signInEmail({
body: { email, password },
asResponse: true,
headers: request.headers
});
Stuart B
Stuart B2h ago
Yes.

Did you find this page helpful?