Session apis returns null on client and server
The session api returns a null value in sveltekit even after successful account creation and login with all its associated functions being unresponsive(session list && response being unresponsive .Below is a sample snippet on the hooks.server.ts in sveltekit
import { fail } from '@sveltejs/kit';
import type { Actions, PageServerLoad } from './$types';
import { superValidate, message } from 'sveltekit-superforms';
import { formSchema } from '$lib/zod/schema';
import { zod } from 'sveltekit-superforms/adapters';
import { auth } from '$lib/auth';
import { APIError } from 'better-auth/api';
export const load: PageServerLoad = async () => {
return {
form: await superValidate(zod(formSchema))
};
};
export const actions = {
default: async (event) => {
const form = await superValidate(event, zod(formSchema));
const { name, email, password } = form.data;
if (!form.valid) {
return fail(400, {
form
});
}
try {
const user = await auth.api.signUpEmail({
body: {
name: name,
email: email,
password: password
}
});
const session = await auth.api.getSession({ headers: event.request.headers });
console.log(user, session?.user.name);
} catch (error) {
if (error instanceof APIError) {
console.log(error.message, error.status);
}
}
return message(form, 'Valid form!');
}
} satisfies Actions;
0 Replies