Next.js Discord

Discord Forum

Stateless auth store - SSR

Answered
Norwegian Forest Cat posted this in #help-forum
Open in Discord
Norwegian Forest CatOP
Hey,

this is probably a very newbie question - related to SSR / SPA.
I am using Pocketbase SDK in my NextJS app.

If I use Pocketbase SDK like this, am I getting always different instance? (I need to have it stateless from my understanding as I am not working on SPA now but SSR, right?)
// pocketbase.ts
import PocketBase from "pocketbase";

const pb = new PocketBase(process.env.NEXT_PUBLIC_PB_URL);

if (process.env.NODE_ENV === "development")
    pb.autoCancellation(false);

export default pb;

// someComponent.tsx
import pb from "./pocketbase";
// Do stuff on pb instance - for example access `pb.authStore`
Answered by Ray
and set the cookie with authStore.exportToCookie after authenticated
View full answer

10 Replies

Oriental chestnut gall wasp
same question here, I'm scared that if I do some auth, it mutates the pb instance, and some other user loading a component using pb will get the same - shared - instance that is logged in as the first user
no, you shouldnt be getting different instance if you're using the serverless architecture from Vercel
you need to use the singleton pattern to make sure pb only get instanced once
I'm using pocketbase with nextj 14 without any issue. just make sure to run authStore.loadFromCookie before fetching the data
and set the cookie with authStore.exportToCookie after authenticated
Answer
I also refreshing the cookie in middleware
@aardani you need to use the singleton pattern to make sure pb only get instanced once
Oriental chestnut gall wasp
yeah but then if you authanticate as a user on that singleton it will be authenticated for all users as that user
@Ray and set the cookie with `authStore.exportToCookie` after authenticated
Oriental chestnut gall wasp
thanks for the tips, I'll do that :)