#access event.locals from a server function

6 messages · Page 1 of 1 (latest)

kindred elbow
#

Hey, I'm new to Solid.js and SolitStart.
I'm trying to implement PocketBase auth and got middleware setup:

export default createMiddleware({
    onRequest: [
        async ({ locals, response, request }) => {
            locals.pb = new PocketBase(process.env.SECURE_PB_URL);

            locals.pb.authStore.loadFromCookie(
                request.headers.get('cookie') || ''
            );

            try {
                locals.pb.authStore.isValid &&
                    (await locals.pb.collection('users').authRefresh());
            } catch {
                locals.pb.authStore.clear();
            }

            response.headers.append(
                'set-cookie',
                locals.pb.authStore.exportToCookie()
            );
        }
    ]
});

is it possible to access event.locals inside a server function?

thorn river
#

Have you tried getRequestEvent() ?

import { getRequestEvent } from "solid-js/web"

const getUsers = cache(async () => {
  'use server';
  const event = getRequestEvent()
  console.log(event.locals)
  ...
}, 'users');
kindred elbow
#

that was it thanks!

kindred elbow
#

im actually having issue with this line ```ts
response.headers.append(
'set-cookie',
locals.pb.authStore.exportToCookie()
);

#

it doesnt seem to actually set the cookie

wind oar
# kindred elbow it doesnt seem to actually set the cookie

Use appendHeader with event.nativeEvent and see if that works.

GitHub

SolidStart, the Solid app framework. Contribute to solidjs/solid-start development by creating an account on GitHub.

GitHub

The Full Stack JavaScript SDK. Contribute to nksaraf/vinxi development by creating an account on GitHub.