access event.locals from a server function

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()
);
}
]
});
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?
4 Replies
duk
duk6mo ago
Have you tried getRequestEvent() ?
import { getRequestEvent } from "solid-js/web"

const getUsers = cache(async () => {
'use server';
const event = getRequestEvent()
console.log(event.locals)
...
}, 'users');
import { getRequestEvent } from "solid-js/web"

const getUsers = cache(async () => {
'use server';
const event = getRequestEvent()
console.log(event.locals)
...
}, 'users');
Shiba Bop
Shiba BopOP6mo ago
that was it thanks! im actually having issue with this line
response.headers.append(
'set-cookie',
locals.pb.authStore.exportToCookie()
);
response.headers.append(
'set-cookie',
locals.pb.authStore.exportToCookie()
);
it doesnt seem to actually set the cookie
peerreynders
peerreynders6mo ago
- FetchEvent also contains nativeEvent. - vinxi/http re-exports unjs H3 appendHeader(). Use appendHeader with event.nativeEvent and see if that works.
GitHub
solid-start/packages/start/src/server/types.ts at 678d9acbc0bb669f8...
SolidStart, the Solid app framework. Contribute to solidjs/solid-start development by creating an account on GitHub.
Response - h3
Utilities to send response headers and data
GitHub
vinxi/packages/vinxi/runtime/http.js at e9f6a235c67bca6a904929e36ef...
The Full Stack JavaScript SDK. Contribute to nksaraf/vinxi development by creating an account on GitHub.
Shiba Bop
Shiba BopOP6mo ago
appendHeader and getHeader did help somewhat but it was an order of operations issue, I ended up doing this:
export default createMiddleware({
onRequest: [
async ({ locals, nativeEvent }) => {
locals.pb = new PocketBase(process.env.SECURE_PB_URL);

locals.pb.authStore.loadFromCookie(
getHeader(nativeEvent, 'cookie') || ''
);

try {
locals.pb.authStore.isValid &&
(await locals.pb.collection('users').authRefresh());
} catch {
locals.pb.authStore.clear();
}
}
],
onBeforeResponse: [
async ({ locals, request, nativeEvent }) => {
appendHeader(
nativeEvent,
'set-cookie',
locals.pb.authStore.exportToCookie({
secure: false
})
);
}
]
});
export default createMiddleware({
onRequest: [
async ({ locals, nativeEvent }) => {
locals.pb = new PocketBase(process.env.SECURE_PB_URL);

locals.pb.authStore.loadFromCookie(
getHeader(nativeEvent, 'cookie') || ''
);

try {
locals.pb.authStore.isValid &&
(await locals.pb.collection('users').authRefresh());
} catch {
locals.pb.authStore.clear();
}
}
],
onBeforeResponse: [
async ({ locals, request, nativeEvent }) => {
appendHeader(
nativeEvent,
'set-cookie',
locals.pb.authStore.exportToCookie({
secure: false
})
);
}
]
});
and it works!
Want results from more Discord servers?
Add your server