pocketbase/js-sdk

Question (SvelteKit): what is the best way to forward realtime subscription from?

ramadhanafif opened this issue · 2 comments

Hi, this is more of a question instead on an issue.

The setup is, I have a svelte project that uses SSR and CSR.

In the Server

In the server, I start by fetch current data from the DB: using hooks.server.ts to define the new PocketBase(URL), and fetch the data in page.server.ts.

In the Client

And after the page is rendered on the client side, I want to subscribe to realtime update. Then, any new data is automatically rendered in the client side.

This is a straightforward setup, however, I need some help on how to enable authentication for this. Particularly if I want to enable admin-level authentication.

My current idea is to authenticate on the server side, and then forward the AdminAuthResponse and the pb.baseUrl. This solution will give client the admin token and the url, then it can create a new PocketBase instance just for subscribing the data.

I tried it without authentication, so far it worked. But when I add admin authentication, it seems to stopped working? And when I checked the pb.authStore.isValid, it says true on server, but false on client. Why?

What do you think? Any better solution to do this?

Without a minimal code sample showing how you are initializing the SDK client and populating the store, I don't think I can help you. If you use cookies then it may have to be exported with httpOnly: false to be able to access it on the client-side.

In any case I don't recommend forwarding the the admin token to the client unless you are really sure that this is what you want.

Keep in min that admins have access and modify everything (including creating other admins or modifying the db schema).

If you have further questions, please use the Q&A discussions in the main repo.

Thanks for the assistance, I'll try to get on more details and put them in Q&A section.

Edit: it seems that a similar question has been asked in here