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

Cookies don't work when using an Elysia instance #126

Open
facundo-villa opened this issue Aug 30, 2024 · 3 comments
Open

Cookies don't work when using an Elysia instance #126

facundo-villa opened this issue Aug 30, 2024 · 3 comments

Comments

@facundo-villa
Copy link

Cookies don't get sent to subsequent requests when using Eden with an Elysia instance. Even when credentials: include is set.

I'm using Elysia version 1.1.9 and Eden 1.1.2

I expect set credential cookies to be sent to subsequent request, as to be able to correctly test endpoints that need authentication and/or tokens.

Cookies do work correctly when performing actual requests to the server from another HTTP client.

Sample code:

test("...", async () => {
	...

	const app = server(db); // Creates an Elysia instance
	const client = treaty(app, { fetch: { credentials: 'include' } });

       ...

	const response = await client.api.auth.token.post({ email: "..." });

	const users = await client.api.users.get(); // <-- This fails because cookies are not set

       ...
});
@facundo-villa
Copy link
Author

@SaltyAom Could you look at this? This is blocking a lot of my tests.

@draylegend
Copy link

@facundo-villa I'm using Elysia v1.1.26 and Eden v1.1.3.

I have set up the /auth/login and /me routes, and everything seems to be working well.

/auth/login Route

export const authRoute = new Elysia().post('/auth/login', async ({ body, cookie: { auth } }) => {
  const { accessToken } = await someClient.login(body.email, body.password);

  auth.set({ priority: 'high', value: accessToken });

  return { ok: true };
}, {
  body: t.Object({ email: t.String(), password: t.String() }),
  cookie: t.Cookie({
    auth: t.Optional(t.String()),
  }, {
    httpOnly: true,
    sameSite: 'strict',
    secrets: process.env['COOKIE_SECRET'],
    secure: true,
    sign: ['auth'],
  }),
});

/me Route

export const meRoute = new Elysia().get('/me', ({ cookie: { auth } }) => {
  console.log(auth);

  const dataFromDatabase = ...;

  return dataFromDatabase;
}, {
  cookie: t.Cookie({ auth: t.String() }, {
    httpOnly: true,
    sameSite: 'strict',
    secrets: process.env['COOKIE_SECRET'],
    secure: true,
    sign: ['auth'],
  }),
});

Try updating the packages to the latest versions.

@facundo-villa
Copy link
Author

This is still fails for me. I might've not made it abundantly clear in my original posts, but this issue arises during integration tests. The actual application web application does work correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants