SvelteKit Issues

Hi, I've been following the SvelteKit guide and I am unable to get the session on the client side. server auth
export const auth = betterAuth({
database: prismaAdapter(db, {
provider: 'postgresql'
}),
emailAndPassword: {
enabled: true
},
secret: process.env.BETTER_AUTH_SECRET!,
baseURL: process.env.BETTER_AUTH_URL!,
// trustedOrigins: [process.env.BETTER_AUTH_URL!],
plugins: [sveltekitCookies(getRequestEvent)],
});
export const auth = betterAuth({
database: prismaAdapter(db, {
provider: 'postgresql'
}),
emailAndPassword: {
enabled: true
},
secret: process.env.BETTER_AUTH_SECRET!,
baseURL: process.env.BETTER_AUTH_URL!,
// trustedOrigins: [process.env.BETTER_AUTH_URL!],
plugins: [sveltekitCookies(getRequestEvent)],
});
client auth
export const authClient = createAuthClient({
// baseURL: PUBLIC_APP_URL
});
export const authClient = createAuthClient({
// baseURL: PUBLIC_APP_URL
});
hooks.server.ts
export async function handle({ event, resolve }) {
const session = await auth.api.getSession({
headers: event.request.headers
});

if (session) {
event.locals.session = session.session;
event.locals.user = session.user;
}

return svelteKitHandler({ event, resolve, auth, building });
}
export async function handle({ event, resolve }) {
const session = await auth.api.getSession({
headers: event.request.headers
});

if (session) {
event.locals.session = session.session;
event.locals.user = session.user;
}

return svelteKitHandler({ event, resolve, auth, building });
}
Whenever I try to get the session on the client side, I get this error: [404] GET /api/auth/get-session. I am able to interact with the server auth and can create accounts. I have tried adding trustedDomains in the server auth then added the baseURL into the client auth and I no longer get the same issue (commented out), instead I get Cross-Origin Request Blocked errors.
Solution:
After looking it into, I realised the issue was that I was running my server on another machine. If you have a similar workflow, you have to set the url to the local IP of the machine that is running the server. Make sure to add the trustedDomains and baseURL parameters
Jump to solution
1 Reply
Solution
popaad
popaad3w ago
After looking it into, I realised the issue was that I was running my server on another machine. If you have a similar workflow, you have to set the url to the local IP of the machine that is running the server. Make sure to add the trustedDomains and baseURL parameters

Did you find this page helpful?