Replies: 1 comment
|
Confirmed from the source, this isn't configurable today, it's built into how // New session store in response cookies
if (!session.id) {
session.id = config.generateId?.() ?? (config.crypto || crypto).randomUUID()
session.createdAt = Date.now()
await updateSession(event, config) // this is what writes Set-Cookie
}There's no flag to say "don't create one if it doesn't exist", the moment there's no session to load, it mints an id and calls One thing that might simplify your workaround though: instead of calling import { getCookie } from 'h3'
function hasSession(event: H3Event, config: SessionConfig) {
const sessionName = config.name || 'h3'
if (config.sessionHeader !== false) {
const headerName = typeof config.sessionHeader === 'string'
? config.sessionHeader.toLowerCase()
: `x-${sessionName.toLowerCase()}-session`
if (event.req.headers.get(headerName)) return true
}
return Boolean(getCookie(event, sessionName))
}That mirrors exactly what |
Uh oh!
There was an error while loading. Please reload this page.
both
useSessionandgetSessionfromh3will set a cookie in any case and it feel strange to have a getter function mutating state.Is there a way to only get a session if it exists without writing a new cookie? In some countries you cannot use cookie without user consent.
I'm manually removing the cookie by myself after checking the session using
clearResponseHeaders(events, 'set-cookie')related: https://github.com/orgs/h3js/discussions/1043
thanks
All reactions