Sveltekit with PocketBase - Cannot use relative URL with global fetch... #9586
-
Dear Svelte Community, I'm currently having issues with implementing authentication using PocketBase in a SvelteKit project. I'm using the following code for authentication: src/routes/login/+page.server.ts import { error, redirect } from '@sveltejs/kit';
import type { Actions } from './$types';
export const actions = {
login: async ({ locals, request }) => {
const { email, password } = Object.fromEntries(await request.formData()) as {
email: string;
password: string;
};
try {
await locals.pb.collection('users').authWithPassword(email, password);
} catch (e) {
console.error(e);
throw e;
}
throw redirect(303, '/');
}
} satisfies Actions; However, when I try to login, I get the following error message:
I'm not sure what the issue is, as I've used similar code in a previous project that worked fine. I'm wondering if anyone has experienced a similar issue with PocketBase and SvelteKit, and if so, how they were able to resolve it. Any help or suggestions would be greatly appreciated. Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
@ArthurTimofey I just had the same issue and it turned out to be in how I was using my environment variables. Instead of declaring it like this |
Beta Was this translation helpful? Give feedback.
-
I just had the same issue while the same code and setup was used hours before without any issues. // error: // no error: |
Beta Was this translation helpful? Give feedback.
-
I faced the same problem with this error:
The issue was that the URL lacked |
Beta Was this translation helpful? Give feedback.
@ArthurTimofey I just had the same issue and it turned out to be in how I was using my environment variables. Instead of declaring it like this
PB_URL="https://somepocketbaseurl"
, you would want to useVITE_PB_URL
(more here).And then in your
src/hooks.server.ts
:event.locals.pb = new PocketBase(import.meta.env.VITE_PB_URL);
This worked for me, I hope it helps you :).