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

Set cookies #382

Closed
1 task
MickL opened this issue Apr 8, 2024 · 12 comments
Closed
1 task

Set cookies #382

MickL opened this issue Apr 8, 2024 · 12 comments

Comments

@MickL
Copy link

MickL commented Apr 8, 2024

Describe the feature

I have a login route that returns a session cookie but when sending subsequents requests the cookie is not set:

const loginResponse = await ofetch.raw('/login', {
      method: 'POST',
      body: {
        email: '[email protected]',
        password: 'test123',
      },
    });
expect(loginResponse.status).toBe(204);

const securedRouteResponse = await ofetch.raw('/admin/products');
expect(securedRouteResponse).toBe(200); // -> 403

Unfortunately the cookie is never set. I tried to set option credentials: 'include' on both requests with no success. Am I missing something?

Additional information

  • Would you be willing to help implement this feature?
@87xie
Copy link

87xie commented Apr 9, 2024

Checking the Set-Cookie attributes, such as Path, Domain, Expires, etc, in the server response headers of the /login endpoint to ensure the cookie is stored correctly

@MickL
Copy link
Author

MickL commented Apr 9, 2024

Yes it works fine in browser and in Postman

@duy-dev
Copy link

duy-dev commented Apr 23, 2024

I also have the same problem, the application runs fine locally but when deployed to Firebase it cannot get cookies 😥

@sonicjhon1
Copy link

Try with

const loginResponse = await ofetch.raw('/login', {
      method: 'POST',
      body: {
        email: '[email protected]',
        password: 'test123',
      },
      credentials: "include",
});

@MickL
Copy link
Author

MickL commented Apr 26, 2024

You mean credentials: include? Thats what I tried as I wrote in the initial issue

@sonicjhon1
Copy link

sonicjhon1 commented Apr 26, 2024

You mean credentials: include? Thats what I tried as I wrote in the initial issue

Yeah my bad, did you try it with access-control-allow-credentials header set to true in the server?

@MickL
Copy link
Author

MickL commented Apr 26, 2024

It works in the browser and with Postman, just not in the test or an empty node script with ofetch.

@pratik149
Copy link

pratik149 commented Jun 5, 2024

Facing the same issue, runs fine locally but not in production. Did anyone figure this out? :(

@Aareksio
Copy link

fetch by design is not managing the cookies for you


What you are trying to accomplish is creating a stateful client, which shares state between subsequent requests. fetch, and by extension ofetch, is not designed to do this. Each request is independent. To accomplish your goal, a separate concept of "cookie jar" is required - a storage instance which is going to read response headers, persist cookies and set them on future requests. You can write it yourself or try open source pacakges.

I imagine using fetch-cookie package with ofetch should look like this:

import { $fetch } from 'ofetch'
import makeFetchCookie from 'fetch-cookie'

const fetchWithCookies = makeFetchCookie($fetch.native)
const client = $fetch.create({ fetch: fetchWithCookies })

@MickL
Copy link
Author

MickL commented Jun 17, 2024

Thanks for the info!

Can we treat this issue as a feature request then? So ofetch will behave as a cookie jar, automatically setting cookies @pi0 ?

@duy-dev
Copy link

duy-dev commented Jun 19, 2024

I also have the same problem, the application runs fine locally but when deployed to Firebase it cannot get cookies 😥

After many days of researching, I also found the problem. Firebase only accepts one cookie named __session. Hope this information is useful to everyone. Details on this can be found here: https://firebase.google.com/docs/hosting/manage-cache?hl=en#using_cookies

@pi0
Copy link
Member

pi0 commented Aug 28, 2024

@Aareksio is well explained, ofetch is stateless by design.

However, i see good use cases for stateful cookie (most importantly for testing), therefore made new proposal (~> #435). With stateful instance, we can also register a hook that implements cookie support.

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

No branches or pull requests

7 participants