PocketBase Auth with Context doesn't track store

I'm trying to create a PocketBase AuthContext that I may add to my app, so that in the future if I wish to add more routes I can access the data from all the routes. However this is getting on my nerves, because the store isn't tracking when the pb.authStore changes (creation / deletion / updated), that is except when HMR kicks in, then it refreshes. And the weirdest part is I can log it on the console... Here is the code:
const AuthContext = createContext<
{ token: string; model: AuthModel } | string
>();

export function AuthProvider(props: { children: JSX.Element }) {
//kind of a singleton, supposedly (in my mind, as this will only run once)
const pb = createMemo(() => new PocketBase("http://127.0.0.1:8090"));

createEffect(() => console.log(pb().authStore.model));

//store for auth token and user data
const [{ token, model }, setState] = createStore({
model: pb().authStore.model,
token: pb().authStore.token,
});

//subscribing to changes on the authStore so that it updates the solid store
createEffect(() => {
pb().authStore.onChange((tok, mod) => {
setState(reconcile({ model: mod, token: tok }));
console.log("authStore changed: ", model);
});
});
return (
<AuthContext.Provider value={{ token, model }}>
{props.children}
</AuthContext.Provider>
);
}

export function useAuth() {
return useContext(AuthContext);
}
const AuthContext = createContext<
{ token: string; model: AuthModel } | string
>();

export function AuthProvider(props: { children: JSX.Element }) {
//kind of a singleton, supposedly (in my mind, as this will only run once)
const pb = createMemo(() => new PocketBase("http://127.0.0.1:8090"));

createEffect(() => console.log(pb().authStore.model));

//store for auth token and user data
const [{ token, model }, setState] = createStore({
model: pb().authStore.model,
token: pb().authStore.token,
});

//subscribing to changes on the authStore so that it updates the solid store
createEffect(() => {
pb().authStore.onChange((tok, mod) => {
setState(reconcile({ model: mod, token: tok }));
console.log("authStore changed: ", model);
});
});
return (
<AuthContext.Provider value={{ token, model }}>
{props.children}
</AuthContext.Provider>
);
}

export function useAuth() {
return useContext(AuthContext);
}
No description
3 Replies
gmnss1917
gmnss191710mo ago
PS: I wrapped my Routes with the AuthProvider
mdynnl
mdynnl10mo ago
1. you don't need createMemo, createEffect here as pocketbase is external to solid and doesn't(just a hunch) use any solid primitives(signals, memo) 2. don't destructure store { token, model } (also applies to useAuth's return) 3. instead directly pass the state (store) to context 4. also a good practice to unsub using onCleanup 5. reconcile might be unnecessary if model is a completely different object, doing setState({ model: mod, token: tok }) should replace the old one with the new one completely
gmnss1917
gmnss191710mo ago
thanks for the help bruv it seems to working now !
Want results from more Discord servers?
Add your server
More Posts
How can I populate HTML on the server with db data before sending it to the clientHello - i am new to solidstart and today as I was attempting to port a nextjs app into solidstart I Proper implementation of a timer/stopwatchI am struggling to understand how to properly implement a timer in a reactive library/framework likeNo Vercel LogsHi! I've deployed a solid-start app using Auth.js which I originally generated using create-jd-app tPrevent re-render <table> element when changing array in store.is it possible to prevent re-render whole table element when i splice element from array in store? TIs <HydrationScript/> required even if I'm not utilizing SolidJS' SSR?In my project, the `Uncaught ReferenceError: _$HY is not defined` is shown when I'm not using `<HydrBuilding with SSR false starts (and doesn't close) listenersIn my app I create a WebsocketServer on Port 3001. If I run `npm run build` with default SSR settingSolidJS: Manually using SSR with v8?Hi, I am working on a project which has a PHP back-end, but which has access to a PHP V8 (https://gHow to save canvas state to backend and load it later?How can I load the state and save it to the backend later?Is this really the most ergonomic way to preserve searchParams with <A>?I've got what I imagine is a pretty common pattern: displaying a list of entities on the left, each How to instruct a component to reset a state it holds?I have a form looking like this (simplified code): ```jsx <Form> <TextField value={store.foo} />