Skip to content
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

Unable to set more than 1 cookie when using redirect() #637

Closed
doroved opened this issue May 13, 2024 · 4 comments
Closed

Unable to set more than 1 cookie when using redirect() #637

doroved opened this issue May 13, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@doroved
Copy link

doroved commented May 13, 2024

What version of Elysia.JS is running?

^1.0.18

What platform is your computer?

Darwin 23.4.0 arm64 arm

What steps can reproduce the bug?

If you set more than 1 cookie, it will not be set.

import { Elysia, redirect } from "elysia";

new Elysia()
  .get("/", () => "yay")
  .get("/redirect", ({ cookie: { name, name2 } }) => {
    name.value = 'value'
    name2.value = 'value2'
    return redirect("https://youtu.be/whpVWVWBW4U?&t=8");
  })
  .listen(8080);

What is the expected behavior?

>1 cookies must be successfully set when redirect() is used

What do you see instead?

This is what the server headers look like when 1 or 2 cookies are set
image

Additional information

UPD: It turns out that if you use set.redirect, there are no problems.

import { Elysia } from "elysia";

new Elysia()
  .get("/", () => "yay")
  .get("/redirect", ({ cookie: { name, name2 }, set }) => {
    name.value = 'value'
    name2.value = 'value2'

    set.redirect = 'https://youtu.be/whpVWVWBW4U?&t=8'
  })
  .listen(8080);

image

@doroved doroved added the bug Something isn't working label May 13, 2024
@bogeychan
Copy link
Contributor

bogeychan commented May 13, 2024

@SaltyAom, the main cause is that this function returns a Headers object:

export const parseSetCookies = (headers: Headers, setCookie: string[]) => {

elysia/src/handler.ts

Lines 152 to 153 in 968650b

set.headers = parseSetCookies(
new Headers(set.headers) as Headers,

which cannot be used in such a spreading operation:

const inherits = { ...set.headers }

inherits is allways {}


Is this parsing step even needed?

@bogeychan
Copy link
Contributor

bogeychan commented May 13, 2024

In addition to redirecting, this bug also occurs when returning raw Response objects:

import { Elysia } from "elysia";

new Elysia()
  .get("/", () => "yay")
  .get("/redirect", ({ cookie: { name, name2 }, set }) => {
    name.value = 'value'
    name2.value = 'value2'

    return new Response('yay')
  })
  .listen(8080);

@SaltyAom
Copy link
Member

Fixed on a4d74d5, published on 1.0.19

@doroved
Copy link
Author

doroved commented May 14, 2024

Fixed on a4d74d5, published on 1.0.19

I checked the solution to my problem + #637 (comment), it's ok, thanks

@doroved doroved closed this as completed May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants