Filippo
Filippo
BABetter Auth
Created by Filippo on 4/23/2025 in #help
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
12 replies