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

Cookie Domain/Expiration are Undefined #819

Closed
topether21 opened this issue Sep 9, 2024 · 3 comments
Closed

Cookie Domain/Expiration are Undefined #819

topether21 opened this issue Sep 9, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@topether21
Copy link

topether21 commented Sep 9, 2024

What version of Elysia is running?

1.1.12

What platform is your computer?

Darwin 24.0.0 arm64 arm

What steps can reproduce the bug?

Create a simple Elysia app

import { Elysia } from "elysia";
import { isHtml } from '@elysiajs/html'

const app = new Elysia()

app.get('/', () => '<h1>Hello World</h1>', {
  afterHandle({ response, set, cookie }) {
    console.log("afterHandle", cookie)
    cookie.my_cookie.expires = new Date(Date.now() + 1000 * 60 * 60 * 24)
    cookie.my_cookie.value = 'my_cookie_value'
    console.log("cookie value", cookie.my_cookie.value)
    console.log("cookie expires", cookie.my_cookie.expires)
    if (isHtml(response))
      set.headers['content-type'] = 'text/html; charset=utf8'
  }
})

app.listen(3000);

console.log(
  `🦊 Elysia is running at http://${app.server?.hostname}:${app.server?.port}`
);
1. Run the application.
2. Check the cookie handling logic.
3. Observe the output for the cookie expiration.

What is the expected behavior?

The cookie expires should have a defined value indicating when the cookie will expire.

What do you see instead?

I encountered an issue where the cookie expiration is returning as undefined, which is not the expected behavior.
The output shows cookie expires undefined, which suggests that the expiration date is not being set or retrieved correctly.

image

Have you try removing the node_modules and bun.lockb and try again yet?

Yes

@topether21 topether21 added the bug Something isn't working label Sep 9, 2024
@topether21 topether21 reopened this Sep 12, 2024
@topether21 topether21 changed the title Cookie Expiration Undefined Cookie Domain/Expiration are Undefined Sep 12, 2024
@PackHubTH
Copy link

Can you try setting cookie value first before other attributes, it might not have been created yet when you set expires

@topether21
Copy link
Author

topether21 commented Sep 13, 2024

Thank you, @PackHubTH l. Let me improve the example. It sets the expiration, and the browser reads the value, but the service does not:

import { Elysia } from "elysia";
import { isHtml } from '@elysiajs/html'

const app = new Elysia()

app.get('/', () => '<h1>Hello World</h1>', {
  afterHandle({ response, set, cookie }) {
    console.log("afterHandle", cookie)
    cookie.my_cookie.value = 'my_cookie_value'
    cookie.my_cookie.expires = new Date(Date.now() + 1000 * 60 * 60 * 24)
    console.log("cookie value", cookie.my_cookie.value)
    console.log("cookie expires", cookie.my_cookie.expires)
    if (isHtml(response))
      set.headers['content-type'] = 'text/html; charset=utf8'
  }
})

app.get('/a', ({ cookie: { my_cookie } }) => {
  console.log("cookie value", my_cookie)
  console.log("cookie expires", my_cookie.expires)
  console.log("cookie domain", my_cookie.domain)
  return {
    message: { my_cookie }
  }
})
app.listen(3000);

console.log(
  `🦊 Elysia is running at http://${app.server?.hostname}:${app.server?.port}`
);

Monosnap localhost:3000:a 2024-09-13 10-56-40

SaltyAom added a commit that referenced this issue Sep 18, 2024
@SaltyAom
Copy link
Member

Fixed and published under 1.1.13 with test case included
see 1.1.12...1.1.13

If this doesn't fix the issue, feel free to tag me or reopen the issue.

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