-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a cookies
interface to RequestEvent
and LoadServerEvent
#6540
Comments
Might be nice to throw in |
Marking as a breaking change since we'd probably want to prevent |
I'm split on this. It adds a new API for one specific use case - I don't see others outside this. There was the idea in another issue to be able to pass data back into load, maybe something like this can be used here, too, so we can solve both these use cases with one API. |
It is a specific use case, but it's one that isn't currently served.
It's handled very differently at an implementation level. So I don't think we'd be going rogue by treating it differently at an API level (or rather, more differently than we already currently do). The other argument in favour of this is that every non-trivial app is going to need to parse cookies at one point, and this... // src/routes/+layout.server.js
export async function load({ cookies }) {
const user = await db.getUserFromSessionId(cookies.get('sessionid'));
return { user };
} ...just seems a lot nicer than this: import * as cookie from 'cookie';
// src/routes/+layout.server.js
export async function load({ request }) {
const cookies = cookie.parse(request.headers.get('cookie') ?? '');
const user = await db.getUserFromSessionId(cookies.sessionid);
return { user };
} |
I like this. Thoughts: Depending on the browser Sign/Verify -- yes, please. It might require changes to some people's code (e.g. I use JWTs since they are easy to sign.) But that's fine if it means I don't have to think about whether or not to sign. It'd be nice to have the API catch the case where a cookie is over the 4K limit after |
It'll just be |
Regardless of whether this proposition passes or not, I believe someone should still be able to We should not prevent the devs to set any headers they want. Granted, it creates confusion and it would be a bad practise; so we could issue a warning on |
* add new cookies API - closes #6540 * update tests and docs * tidy up * secure by default * fixes * Update packages/kit/src/runtime/server/cookie.js * insecure cookies for benefit of safari
Describe the problem
In #5748, we introduced the
setHeaders
API:Recent explorations (see the 'cookie handling' section in particular) suggest the API falls short. We need a way to set a cookie inside an action that can be read during the subsequent
load
, and while we could make it work withsetHeaders
it would involve some very unexpected behaviour (we would basically need to interceptrequest.headers.get('cookie')
and sub in a value that included theset-cookie
, which is a bad amount of magic).Describe the proposed solution
We add a new
cookies
interface:There is an experimental standard
CookieStore
interface, but I don't think we should use it. It's designed for use in browsers, which means all the methods are asynchronous — needlessly, in our case — and there's achange
event that doesn't really make sense in our context. More important, it doesn't expose anhttpOnly
option (because that wouldn't make sense for client-side JavaScript), whereas I thinkhttpOnly
is the only sensible default.The proposed API:
If you're familiar with the cookie package, this API will seem very familiar — it's what we'd use under the hood.
Alternatives considered
Relying on people populating
event.locals
in actions, which feels unnecessarily restrictive.Intercepting (and changing)
request.headers.get('cookie')
, which feels like too much magic.Importance
would make my life easier
Additional Information
No response
The text was updated successfully, but these errors were encountered: