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 forwarding in load function #606

Closed
1 of 2 tasks
GuillaumeNury opened this issue Aug 19, 2023 · 2 comments · Fixed by #614
Closed
1 of 2 tasks

Cookies forwarding in load function #606

GuillaumeNury opened this issue Aug 19, 2023 · 2 comments · Fixed by #614
Labels
enhancement New feature or request

Comments

@GuillaumeNury
Copy link

Which scope/s are relevant/related to the feature request?

router

Information

Hello!

I use cookies to store authentication token. The fetch function in load does not use original request headers.

Given the following code:

import type { PageServerLoad } from '@analogjs/router';

export const load = async ({ fetch, req }: PageServerLoad) => {
  console.log('req.url', req.url);
  console.log('req.headers', req.headers);
  return {
    principal: await fetch<{ name: string }>('/api/v1/users/me'),
  };
};

It logs:

req.url /_analog/pages/-index-
 An error occurred: FetchError:  (401 Unauthorized (/api/v1/users/me))
req.headers {
  'content-length': '0',
  'user-agent': 'Mozilla/5.0 (Linux x64) node.js/18.15.0 v8/10.2.154.26-node.25',
  host: 'localhost:5173',
  connection: 'close',
  accept: 'application/json, text/plain, */*',
  'x-forwarded-for': '127.0.0.1',
  'x-forwarded-port': '49226',
  'x-forwarded-proto': 'IPv4'
}

Is there any ways of using cookies in load function ?

Describe any alternatives/workarounds you're currently using

Make API calls from client.

I would be willing to submit a PR to fix this issue

  • Yes
  • No
@GuillaumeNury GuillaumeNury added the enhancement New feature or request label Aug 19, 2023
@brandonroberts
Copy link
Member

Yes, it's possible to do, but we will need to add the event to the PageServerLoad object

Something like below:

import type { PageServerLoad } from '@analogjs/router';

export const load = async ({ fetch, req, event }: PageServerLoad) => {
  const cookies = parseCookies(event);
  console.log('req.url', req.url);
  console.log('req.headers', req.headers);
  return {
    principal: await fetch<{ name: string }>('/api/v1/users/me'),
  };
};

OR

import type { PageServerLoad } from '@analogjs/router';

export const load = async ({ fetch, req, cookies, event }: PageServerLoad) => {
  console.log('req.url', req.url);
  console.log('req.headers', req.headers);
  return {
    principal: await fetch<{ name: string }>('/api/v1/users/me'),
  };
};

@GuillaumeNury
Copy link
Author

Oh! It is that easy 🤦
Thank you for the quick reply and keep the good job on Analog!

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

Successfully merging a pull request may close this issue.

2 participants