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

Elysia swagger set cookie is undefined #1034

Open
ehsansafari opened this issue Jan 26, 2025 · 3 comments
Open

Elysia swagger set cookie is undefined #1034

ehsansafari opened this issue Jan 26, 2025 · 3 comments
Labels
bug Something isn't working

Comments

@ehsansafari
Copy link

What version of Elysia is running?

1.2.10

What platform is your computer?

MINGW64_NT-10.0-22631 3.4.10-87d57229.x86_64 x86_64 unknown

What steps can reproduce the bug?

import { Elysia } from "elysia";
import { swagger } from "@elysiajs/swagger";

const app = new Elysia()
  .use(swagger())
  .get("/hi", ({ cookie: { my_cookie } }) => {
    console.log(my_cookie.value);
    return { message: "hi", my_cookie: my_cookie.value };
  })
  .get("/", () => "Hi Elysia!")
  .listen(3000);

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

When I set it in Elysia swagger it will be empty.

Image

But when I set in Thunder client or shell http I get the value.

Image

What is the expected behavior?

Get the value of cookies

What do you see instead?

No response

Additional information

cookies undefined

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

yes

@ehsansafari ehsansafari added the bug Something isn't working label Jan 26, 2025
@ehsansafari
Copy link
Author

"@elysiajs/swagger": "^1.2.0",

@ehsansafari
Copy link
Author

Image

issues by scalar

@hisamafahri
Copy link
Contributor

hisamafahri commented Jan 30, 2025

Ah, cross-origin cookie. This rabbit-hole cost me a ton of my time in the past.

This is not a problem in the Elysia.js's part (and I think at least not the @elysiajs/swagger's part too) . My recommendations:

  • Use Authorization header instead. You can even improve the swagger config to accommodate that. E.g:
  .use(
    swagger({
      documentation: {
        components: {
          securitySchemes: {
            bearerAuth: {
              type: "http",
              scheme: "bearer",
              bearerFormat: "JWT",
            },
          },
        },
      },
    }),
  )
  • If you need to keep using cookie, set an httpOnly cookie via another endpoint. Then, /hi endpoint should be able to read it automatically. Eg:
const app = new Elysia()
  .use(swagger())
  .get("/hi", ({ request }) => {
    const bearer = request.headers.get("Authorization");
    return { message: "hi", bearer };
  })
  .get("/set_cookie", ({ cookie }) => {
    cookie["my_cookie"].set({
      value: "hello",
      httpOnly: true,
    });

    return { message: "ok" };
  })
  .listen(9000);

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

2 participants