Can't use Pocketbase OAuth in SvelteKit

465 Views Asked by At

I've tried loads of different tactics but I still wasn't able to get it to work.

This is what Pocketbase recommends (https://pocketbase.io/docs/authentication/):

loginWithGoogle: async ({ locals }: { locals: App.Locals }) => {
    await locals.pb.collection('users').authWithOAuth2({ provider: 'google' });
}

^ this is just an action that I have setup for when the user clicks a button on the UI.

I always get this error:

ClientResponseError 0: Something went wrong while processing your request.
    at new ClientResponseError (file:///home/kraktoos/Dev/Projects/pap/web/node_modules/.pnpm/[email protected]/node_modules/pocketbase/dist/pocketbase.es.mjs:1:2306)
    at RecordService.<anonymous> (file:///home/kraktoos/Dev/Projects/pap/web/node_modules/.pnpm/[email protected]/node_modules/pocketbase/dist/pocketbase.es.mjs:1:26687)
    at step (file:///home/kraktoos/Dev/Projects/pap/web/node_modules/.pnpm/[email protected]/node_modules/pocketbase/dist/pocketbase.es.mjs:1:2109)
    at Object.throw (file:///home/kraktoos/Dev/Projects/pap/web/node_modules/.pnpm/[email protected]/node_modules/pocketbase/dist/pocketbase.es.mjs:1:2214)
    at rejected (file:///home/kraktoos/Dev/Projects/pap/web/node_modules/.pnpm/[email protected]/node_modules/pocketbase/dist/pocketbase.es.mjs:1:918)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  url: '',
  status: 0,
  response: {},
  isAbort: false,
  originalError: ReferenceError: EventSource is not defined
      at RealtimeService.initConnect (file:///home/kraktoos/Dev/Projects/pap/web/node_modules/.pnpm/[email protected]/node_modules/pocketbase/dist/pocketbase.es.mjs:1:39952)
      at file:///home/kraktoos/Dev/Projects/pap/web/node_modules/.pnpm/[email protected]/node_modules/pocketbase/dist/pocketbase.es.mjs:1:39648
      at new Promise (<anonymous>)
      at RealtimeService.<anonymous> (file:///home/kraktoos/Dev/Projects/pap/web/node_modules/.pnpm/[email protected]/node_modules/pocketbase/dist/pocketbase.es.mjs:1:39546)
      at step (file:///home/kraktoos/Dev/Projects/pap/web/node_modules/.pnpm/[email protected]/node_modules/pocketbase/dist/pocketbase.es.mjs:1:2109)
      at Object.next (file:///home/kraktoos/Dev/Projects/pap/web/node_modules/.pnpm/[email protected]/node_modules/pocketbase/dist/pocketbase.es.mjs:1:2214)
      at file:///home/kraktoos/Dev/Projects/pap/web/node_modules/.pnpm/[email protected]/node_modules/pocketbase/dist/pocketbase.es.mjs:1:1103
      at new Promise (<anonymous>)
      at __awaiter (file:///home/kraktoos/Dev/Projects/pap/web/node_modules/.pnpm/[email protected]/node_modules/pocketbase/dist/pocketbase.es.mjs:1:789)
      at RealtimeService.connect (file:///home/kraktoos/Dev/Projects/pap/web/node_modules/.pnpm/[email protected]/node_modules/pocketbase/dist/pocketbase.es.mjs:1:39418)
}
1

There are 1 best solutions below

0
On

The PocketBase example is for client side authentication. You can't use authWithOAuth2 on the server, only on the client.

If you want to do the authentication on the server it's a lot more involved. You need to build a manual authentication flow. See here.