-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Cookies from header not parsed by AstroCookies #13078
Comments
I can't access the reproduction |
Hello @tstern-bolindalabs. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with |
I forgot to make the github repo public. It should now be accessible. |
From reading, // 👇
if (context.cookies.has('test', { decode })) {
const test = context.cookies.get('test', { decode });
console.log('test:', test?.value);
} else {
....
} This does still seem to be a bug, but at least there is a workaround |
Thank you for response. This would be a workaround. Additional it seems that other |
Currently as soon as you call Here is an example I made of your middleware example that introduces an additional middleware that checks if it needs to be decoded by checking for a prefix. import { defineMiddleware, sequence } from "astro/middleware";
export const encode = (data: unknown) => {
console.log('encode:', data);
const dataSerialized = typeof data === 'string' ? data : JSON.stringify(data);
return `base64_${Buffer.from(dataSerialized).toString('base64')}`;
};
export const decode = (str: string) => {
if(str.startsWith('base64_')){
return Buffer.from(str.replace('base64_', ''), 'base64').toString();
} else {
return decodeURIComponent(str)// this is the default implemented by 'cookie' package
// https://github.com/jshttp/cookie?tab=readme-ov-file#decode
}
};
const initializeOurCookies = defineMiddleware((context,next)=>{
context.cookies.get('doesnt-matter', {decode}) // just need to have the cookies parsed
next()
})
const testCookies = defineMiddleware( async (context, next) => {
console.log('headers#cookie:', context.request.headers.get('cookie'));
if (context.cookies.has('test')) {
const test = context.cookies.get('test');
console.log('test:', test?.value);
const test2 = context.cookies.get('test2');
console.log('test2:', test2?.value);
} else {
const tomorrow = new Date(Date.now() + 1000 * 60 * 60 * 24);
context.cookies.set('test', 'test_value', { encode, path: '/', expires: tomorrow });
context.cookies.set('test2', 'test_value_2', { path: '/', expires: tomorrow });
}
return next();
})
export const onRequest = sequence(initializeOurCookies, testCookies); Im not sure how the current API should be fixed, but this what I would do for now. |
Astro Info
If this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
When inside of a middleware calling
context.cookies.get('test', { decode })
the decode function is not invoked and the cookie value is still encoded.The cookie is set within the request header. In the linked minimal reproducible example the page must be reloaded to set the cookie.
What's the expected result?
I would expect that
context.cookies.get
will parse and decode cookie values from request header.Link to Minimal Reproducible Example
https://stackblitz.com/~/github.com/tstern-bolindalabs/just-the-basics
Participation
The text was updated successfully, but these errors were encountered: